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).
int maxSub(vector<int> &prices, int start, int end)
{
vector<int> dp(end-start+1);
dp[0] = 0;
int min = prices[start];
for(int i = start+1; i <= end; i++)
{
dp[i-start] = std::max(dp[i-start-1], prices[i]-min);
min = std::min(min, prices[i]);
}
return dp[end-start];
}
int maxProfit(vector<int> &prices)
{
int n = prices.size();
if(n <= 1) return 0;
int result = 0;
for(int i = 1; i < n-1; i++)
{
result = std::max(result,
maxSub(prices, 0, i) + maxSub(prices, i+1, n-1)
);
}
return result;
}
可惜上面代码超时。
int maxProfit2(vector<int> &prices)
{
int n = prices.size();
if(n <= 1) return 0;
int result = 0;
vector<int> head(n, 0);
vector<int> tail(n, 0);
head[0] = 0;
int min = prices[0];
for(int i = 1; i < n; i++)
{
head[i] = std::max(head[i-1], prices[i]-min);
min = std::min(min, prices[i]);
}
tail[n-1] = 0;//tail[i], [i:n-1]
int max = prices[n-1];
for(int i = n-2; i >=0; i--)
{
tail[i] = std::max(tail[i+1], max-prices[i]);
max = std::max(max, prices[i]);
}
for(int i = 0; i < n; i++)
result = std::max(result, head[i] + tail[i]);
return result;
}