The idea is very basic. At most two transactions means we can break at any time point and compute the max revenue before this time point and after this time point. For every possible time point, we choose the maximum.

Note that right_max start from the last time point, which is just like a mirror algorithm from the Best Time to Buy and Sell Stock I

```
int maxProfit(vector<int>& prices) {
vector<int> left_max;
vector<int> right_max;
int n = prices.size();
if(n == 0){
return 0;
}
int cur_min = prices[0];
int max_r = 0;
for(int i = 0; i < n; i++){
max_r = max(max_r, prices[i] - cur_min);
left_max.push_back(max_r);
cur_min = min(cur_min, prices[i]);
}
int cur_max = prices[n-1];
max_r = 0;
for(int i = n-1; i >= 0; i--){
max_r = max(max_r, cur_max - prices[i]);
right_max.insert(right_max.begin(), max_r);
cur_max = max(cur_max, prices[i]);
}
int sum_max = 0;
for(int i = 0; i < n; i++){
sum_max = max(sum_max, left_max[i] + right_max[i]);
}
return sum_max;
}
```