Posted on

Description

Submission

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int hold = INT_MIN, sell = 0, cool= 0;
        
        for(int p: prices) {
            int hold2 = hold, sell2 = sell, cool2 = cool;

            hold = max(hold2, cool2 - p);       // hold a stock or buy
            sell = hold2 + p;
            cool = max(cool2, sell2);
        }

        return max(sell, cool);
    }
};

// buy = max(buy2, cooldown2 - p)        // hold a stock or buy
// sell = buy2 + p
// cooldown = max(cooldown2, sell2)

Leave a Reply

Your email address will not be published. Required fields are marked *