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

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


链接

题目描述

在这里插入图片描述

思路

在这里插入图片描述

代码

class Solution {
    public int maxProfit(int[] prices) {
        if(prices == null || prices.length == 0){
            return 0;
        }
        int n = prices.length;
        int[][] dp = new int[n][2];
        for(int i = 0; i < n ;i++){
            if(i == 0){
                dp[i][0] = 0;
                //max(-infinity,-prices[i])
                dp[i][1] = -prices[i];
                continue;
            }
            if(i == 1){
                dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1]+prices[i]);
                dp[i][1] = Math.max(-prices[0],-prices[1]);
                continue;
            }
        
            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-2][0]-prices[i]);
        }
        return dp[n-1][0];
    }
}
发布了55 篇原创文章 · 获赞 1 · 访问量 862

猜你喜欢

转载自blog.csdn.net/weixin_42469108/article/details/105113348