0123. Best Time to Buy and Sell Stock III
class Solution {
public:
int maxProfit(vector<int>& prices) {
int firstBuy = -prices[0];
int firstSell = 0;
int secondBuy = -prices[0];
int secondSell = 0;
for(int i = 1; i < prices.size(); i++) {
firstBuy = max(firstBuy, -prices[i]);
firstSell = max(firstSell, prices[i] + firstBuy);
secondBuy = max(secondBuy, -prices[i] + firstSell);
secondSell = max(secondSell, prices[i] + secondBuy);
}
return secondSell;
}
};