309. Best Time to Buy and Sell Stock with Cooldown

Posted ymjyqsx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了309. Best Time to Buy and Sell Stock with Cooldown相关的知识,希望对你有一定的参考价值。

https://www.cnblogs.com/grandyang/p/4997417.html

https://blog.csdn.net/qq508618087/article/details/51671504

用buy、sell两个数组表达,注意初始化

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int length = prices.size();
        if(length <= 0)
            return 0;
        vector<int> buy(length+1,0),sell(length+1,0);
        buy[1]=-prices[0];
        for(int i = 2;i <= length;i++){
            buy[i] = max(buy[i-1],sell[i-2] - prices[i-1]);
            sell[i] = max(sell[i-1],buy[i-1] + prices[i-1]);
        }
        return buy[length] > sell[length] ? buy[length] : sell[length];
    }
};

 

以上是关于309. Best Time to Buy and Sell Stock with Cooldown的主要内容,如果未能解决你的问题,请参考以下文章

309. Best Time to Buy and Sell Stock with Cooldown

309. Best Time to Buy and Sell Stock with Cooldown

[动态规划] leetcode 309 Best Time to Buy and Sell Stock with Cooldown

leetcode 309. Best Time to Buy and Sell Stock with Cooldown

算法: 冷却时间买卖股票的最佳时机309. Best Time to Buy and Sell Stock with Cooldown

算法: 加冷冻期时间的买卖股票309. Best Time to Buy and Sell Stock with Cooldown