【leetcode 动态规划 C++】309. Best Time to Buy and Sell Stock with Cooldown

309. Best Time to Buy and Sell Stock with Cooldown

【leetcode 动态规划 C++】309. Best Time to Buy and Sell Stock with Cooldown

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.size() < 2) return 0;
        vector<int> buy(prices.size(), 0);
        vector<int> sell(prices.size(), 0);
        buy[0] = -prices[0];
        for(int ii = 1; ii < prices.size(); ii++) {
            buy[ii] = max(buy[ii-1], sell[max(0, ii-2)] - prices[ii]);
            sell[ii] = max(sell[ii-1], buy[ii-1] + prices[ii]);
        }
        return max(0, max(buy[prices.size()-1], sell[prices.size()-1]));
    }
};
上一篇:【Leetcode】309. 买卖股票的最佳时机含冷冻期


下一篇:309. 最佳买卖股票时机含冷冻期 ( dp )