题目要求:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
找寻一个点j,将原来的price[0..n-1]分割为price[0..j]和price[j..n-1],分别求两段的最大profit。
进行优化:
对于点j+1,求price[0..j+1]的最大profit时,很多工作是重复的,在求price[0..j]的最大profit中已经做过了。
类似于Best Time to Buy and Sell Stock,可以在O(1)的时间从price[0..j]推出price[0..j+1]的最大profit。
但是如何从price[j..n-1]推出price[j+1..n-1]?反过来思考,我们可以用O(1)的时间由price[j+1..n-1]推出price[j..n-1]。
最终算法:
数组l[i]记录了price[0..i]的最大profit,
数组r[i]记录了price[i..n]的最大profit。
已知l[i],求l[i+1]是简单的,同样已知r[i],求r[i-1]也很容易。
最后,我们再用O(n)的时间找出最大的l[i]+r[i],即为题目所求。
class Solution {
public:
int maxProfit(vector<int> &prices)
{
if(prices.size() == 0)
return 0;
int len = prices.size();
int left[len];
int right[len];
memset(left, 0, sizeof(left));
memset(right, 0, sizeof(right));
int buy = prices[0];
for(size_t i = 1; i < len; ++i)
{
left[i] = max(prices[i] - buy, left[i - 1]);
buy = min(prices[i], buy);
}
buy = prices[len - 1];
for(int i = len - 2; i >= 0; --i)
{
right[i] = max(buy - prices[i], right[i + 1]);
buy = max(prices[i], buy);
}
int res = 0;
for(size_t i = 0; i < len; ++i)
{
res = max(res, left[i] + right[i]);
}
return res;
}
};