码迷,mamicode.com
首页 > 其他好文 > 详细

309. Best Time to Buy and Sell Stock with Cooldown

时间:2019-03-07 17:48:30      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:price   com   turn   www.   amp   details   rand   ret   article   

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

标签:price   com   turn   www.   amp   details   rand   ret   article   

原文地址:https://www.cnblogs.com/ymjyqsx/p/10490818.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!