```
class Solution {
public:
int maxProfit(vector<int>& prices) {
vector<int> res(4, 0);
res[0] = INT_MIN;
res[2] = INT_MIN;
for(int i = 0; i < prices.size(); ++i) {
res[3] = max(res[3], res[2] + prices[i]); // Transaction 2: sell
res[2] = max(res[2], res[1] - prices[i]); // Transaction 2: buy
res[1] = max(res[1], res[0] + prices[i]); // Transaction 1: sell
res[0] = max(res[0], -prices[i]); // Transaction 1: buy
}
return max(res[1], res[3]);
}
};
```