代码
/*
* @lc app=leetcode.cn id=123 lang=cpp
*
* [123] 买卖股票的最佳时机 III
* 前后缀分解
*/
// @lc code=start
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
vector<int> f(n + 2);
for (int i = 1, minp = INT_MAX; i <= n; ++i) {
f[i] = max(f[i - 1], prices[i - 1] - minp);
minp = min(minp, prices[i - 1]);
}
int ans = 0;
for (int i = n, maxp = 0; i; i--) {
ans = max(ans, maxp - prices[i - 1] + f[i - 1]);
maxp = max(maxp, prices[i - 1]);
}
return ans;
}
};
// @lc code=end