欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

309. Best Time to Buy and Sell Stock with Cooldown

程序员文章站 2022-06-17 18:25:39
...

题目

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) {
        int buy = INT_MIN;
        int pre_buy;
        int sell = 0;
        int pre_sell = 0;
        for (int i = 0; i < prices.size(); i++) {
            pre_buy = buy;
            buy = max(pre_sell - prices[i], buy);
            pre_sell = sell;
            sell = max(pre_buy + prices[i], sell);
        }
        return sell;
    }
};