动态规划31(Leetcode188买卖股票的最佳时机4)

代码:

我的状态方程:

buy[i][j]=max{buy[i−1][j],sell[i−1][j-1]−price[i]}

题解里的:

buy[i][j]=max{buy[i−1][j],sell[i−1][j]−price[i]}

..没理解题解的  但我的通过了

class Solution {
    public int maxProfit(int k, int[] prices) {
        if(prices.length==0)return 0;
        int n = prices.length;
        k = Math.min(k,n/2);
        if(k==0)return 0;
        int[][] buy = new int[n][k];
        int[][] sell = new int[n][k];
        buy[0][0]=-prices[0];
        sell[0][0]=0;
        for(int i=1;i

你可能感兴趣的:(动态规划,算法)