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

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]));
    }
};

猜你喜欢

转载自blog.csdn.net/m0_37454852/article/details/114193932
今日推荐