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)的。
精简代码:
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;
}
};
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;
}
};
相关文章推荐
- Leetcode_123_Best Time to Buy and Sell Stock III
- [leetcode] 123. Best Time to Buy and Sell Stock III 解题报告
- leetcode 123 Best Time to Buy and Sell Stock III
- LeetCode 121-123 Best Time to Buy and Sell Stock I II III
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
- LeetCode 123 Best Time to Buy and Sell Stock III
- LeetCode123 Best Time to Buy and Sell Stock III
- 【LeetCode】123. Best Time to Buy and Sell Stock III
- leetcode_[python/C++]_121/122/123/188.Best Time to Buy and Sell Stock I/II/III/IV
- LeetCode - 123. Best Time to Buy and Sell Stock III
- leetcode 123. Best Time to Buy and Sell Stock III 最大k次字段和 + DP
- LeetCode 123 Best Time to Buy and Sell Stock III (贪心)
- LeetCode题解:123. Best Time to Buy and Sell Stock III
- LeetCode - 123. Best Time to Buy and Sell Stock III - 思路详解 - C++
- leetcode——123——Best Time to Buy and Sell Stock III
- [Leetcode] 123. Best Time to Buy and Sell Stock III 解题报告
- [leetcode-123]Best Time to Buy and Sell Stock III(c++)
- leetcode 123: Best Time to Buy and Sell Stock III
- LeetCode: Best Time to Buy and Sell Stock III [123]
- 【leetcode】123. Best Time to Buy and Sell Stock III【java】