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