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

吞佛童子2022年6月9日小于 1 分钟

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

难度: 🌕

问题描述

img_3.png


解法

class Solution {
    public int maxProfit(int[] prices) {
        // 思路:
        // dp
        // 未持有非冷冻期 - 持有 - 卖出 - 冷冻期
        int len = prices.length;
        int[][] dp = new int[len][4];
        // 初始化
        dp[0][1] = - prices[0];
        // dp
        for(int i = 1; i < len; i ++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][3]);
            dp[i][1] = Math.max(dp[i - 1][1], Math.max(dp[i - 1][0] - prices[i], dp[i - 1][3] - prices[i]));
            dp[i][2] = dp[i - 1][1] + prices[i];
            dp[i][3] = dp[i - 1][2];
        }
        return Math.max(dp[len - 1][0], Math.max(dp[len - 1][2], dp[len - 1][3]));
    }
}

输出

img_2.png

上次编辑于: 2022/6/20 下午8:24:47
贡献者: liuxianzhishou