您的位置:首页 > 产品设计 > UI/UE

leetcode_question_123 Best Time to Buy and Sell Stock III

2013-09-22 21:59 507 查看
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).
和前两道题比起来,因为限制了2次交易,这道题最难了。
设i从0到n-1,那么针对每一个i,看看在prices的子序列[0,...,i][i,...,n-1]上分别取得的最大利润(第一题)即可。

这样初步一算,时间复杂度是O(n2)。

改进:

改进的方法就是动态规划,那就是第一步扫描,先计算出子序列[0,...,i]中的最大利润,用一个数组保存下来,那么时间是O(n)。

第二步是逆向扫描,计算子序列[i,...,n-1]上的最大利润,这一步同时就能结合上一步的结果计算最终的最大利润了,这一步也是O(n)。

所以最后算法的复杂度就是O(n)的。

int maxProfit(vector<int> &prices) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int length = prices.size();
if(length < 2) return 0;
//profit from 0 to i
int* profitleft = new int[length];
profitleft[0] = 0;
int lowprice = prices[0];
for(int i = 1; i < length; ++i)
{
if(prices[i] > lowprice)
{
int tmp = prices[i] - lowprice;
if(tmp > profitleft[i-1])
profitleft[i] = tmp;
else
profitleft[i] = profitleft[i-1];
}else{
lowprice = prices[i];
profitleft[i] = profitleft[i-1];
}
}
//profit from i to n
int* profitright = new int[length];
profitright[length-1] = 0;
int highprice = prices[length-1];
for(int i = length-2; i >= 0; --i)
{
if(prices[i] < highprice)
{
int tmp = highprice - prices[i];
if(tmp > profitright[i+1])
profitright[i] = tmp;
else
profitright[i] = profitright[i+1];
}else{
highprice = prices[i];
profitright[i] = profitright[i+1];
}
}
//
int max = 0;
for(int i = 0; i < length; ++i)
{
int tmp = profitleft[i] + profitright[i];
if(tmp > max) max = tmp;
}
return max;
}


int maxProfit(vector<int> &prices) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int length = prices.size();
if(length < 2) return 0;
//profit from 0 to i
int* profitleft = new int[length];
profitleft[0] = 0;
int lowprice = prices[0];
for(int i = 1; i < length; ++i)
{
if(prices[i] > lowprice)
{
int tmp = prices[i] - lowprice;
if(tmp > profitleft[i-1])
profitleft[i] = tmp;
else
profitleft[i] = profitleft[i-1];
}else{
lowprice = prices[i];
profitleft[i] = profitleft[i-1];
}
}
int max = profitleft[length-1];
//profit from i to n
int* profitright = new int[length];
profitright[length-1] = 0;
int highprice = prices[length-1];
for(int i = length-2; i >= 0; --i)
{
if(prices[i] < highprice)
{
int tmp = highprice - prices[i];
if(tmp > profitright[i+1])
profitright[i] = tmp;
else
profitright[i] = profitright[i+1];
}else{
highprice = prices[i];
profitright[i] = profitright[i+1];
}
int tmpmax = profitleft[i] + profitright[i];
if(tmpmax > max) max = tmpmax;
}
return max;
}


精简代码:

class Solution {
public:
int maxProfit(vector<int>& prices) {
int sz = prices.size();
if(sz < 2) return 0;
vector<int> lprofit(sz, 0);
vector<int> rprofit(sz, 0);
int lmin = prices[0], rmax = prices[sz-1];
for(int i = 1; i < sz; i++){
lprofit[i] = max(prices[i]-lmin, lprofit[i-1]);
if(prices[i] < lmin) lmin = prices[i];

rprofit[sz-i-1] = max(rmax-prices[sz-i], rprofit[sz-i]);
if(prices[sz-i-1] > rmax) rmax = prices[sz-i-1];
}
int profit = 0;
for(int i=0; i <sz;i++){
profit = max(profit, lprofit[i]+rprofit[i]);
}
return profit;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息