动态规划day07

122. 买卖股票的最佳时机 II

// class Solution {
//     public int maxProfit(int[] prices) {
//         int n = prices.length;
//         int[][] dp = new int[n][2];
//         dp[0][0] = -prices[0];
//         for (int i = 1; i < n; i++) {
//             //下标0持有股票,下标1不持有
//             dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
//             dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
//         }
//         return dp[n - 1][1];
//     }
// }

//滚动数组 只用到了当前和前天 可用第一维长度为2来覆盖
class Solution {
    public int maxProfit(int[] prices) {
        int n = prices.length;
        int[][] dp = new int[2][2];
        dp[0][0] = -prices[0];
        for (int i = 1; i < n; i++) {
            //下标0持有股票,下标1不持有
            dp[i % 2][0] = Math.max(dp[(i - 1) % 2][0], dp[(i - 1) % 2][1] - prices[i]);
            dp[i % 2][1] = Math.max(dp[(i - 1) % 2][1], dp[(i - 1) % 2][0] + prices[i]);
        }
        return dp[(n - 1) % 2][1];
    }
}

 

123. 买卖股票的最佳时机 III

class Solution {
    public int maxProfit(int[] prices) {
        //四种状态 第一次买入 第一次卖出 第二次买入 第二次卖出
        int len = prices.length;
        int[][] dp = new int[len][4];
        dp[0][0] = -prices[0];
        //当天买入卖出,再买入
        dp[0][2] = -prices[0];
        for (int i = 1; i < len; i++) {
            //昨天买入1 今天买入1
            dp[i][0] = Math.max(dp[i - 1][0], -prices[i]);
            //昨天卖出1 昨天买入1今天卖出1
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
            //昨天买入2 昨天卖出1今天买入2
            dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] - prices[i]);
            //昨天卖出2 昨天买入2今天卖出2
            dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] + prices[i]);
        }
        return dp[len - 1][3];
    }
}

 

188. 买卖股票的最佳时机 IV

class Solution {
    public int maxProfit(int k, int[] prices) {
        int len = prices.length;
        if (len == 0 || k == 0) return 0;
        int[][] dp = new int[len][k * 2];
        //第一天买入 第一天买入卖出再买入 以此类推
        for (int i = 0; i < k; i++) {
            dp[0][i * 2] = -prices[0];
        }
        for (int i = 1; i < len; i++) {
            //符号
            int sym = 1;
            dp[i][0] = Math.max(dp[i - 1][0], -prices[i]);
            for (int j = 1; j < k * 2; j++) {
                //例如第一次买入递推式dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
                dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - 1] + (sym * prices[i]));
                sym *= -1;
            }
        }
        return dp[len - 1][k * 2 - 1];
    }
}

 

309. 最佳买卖股票时机含冷冻期

class Solution {
    public int maxProfit(int[] prices) {
        //三种状态 买入 卖出冻结期 卖出非冻结期
        int len = prices.length;
        int[][] dp = new int[len][3];
        //卖出冻结期为0  买入非冻结期对于第一天来说就是没买
        dp[0][0] = -prices[0];
        for (int i = 1; i < len; i++) {
            //继续买入状态  从卖出非冻结到买入状态
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][2] - prices[i]);
            //卖出冻结期,只能从前一天买入状态到今天卖出
            dp[i][1] = dp[i - 1][0] + prices[i];
            //前一天卖出冻结期和卖出非冻结期
            dp[i][2] = Math.max(dp[i - 1][1], dp[i - 1][2]);
        }
        return Math.max(dp[len -1][1], dp[len - 1][2]);
    }
}

 

参考:programmercarl.com

posted @ 2022-06-24 21:31  一梦两三年13  阅读(26)  评论(0)    收藏  举报