leetcode_question_123 Best Time to Buy and Sell Stock III
来源:互联网 发布:驾照考试软件 编辑:程序博客网 时间:2024/05/23 13:16
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)的。
这样初步一算,时间复杂度是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 iint* 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;elseprofitleft[i] = profitleft[i-1];}else{lowprice = prices[i];profitleft[i] = profitleft[i-1];}}//profit from i to nint* 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;elseprofitright[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 iint* 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;elseprofitleft[i] = profitleft[i-1];}else{lowprice = prices[i];profitleft[i] = profitleft[i-1];}}int max = profitleft[length-1];//profit from i to nint* 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;elseprofitright[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_question_123 Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock III
- [Leetcode] Best Time to Buy and Sell Stock III
- leetcode 16: Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- [LeetCode] Best Time to Buy and Sell Stock III
- leetcode 62: Best Time to Buy and Sell Stock III
- [LeetCode] Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- 【leetcode】Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- LeetCode - Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- LeetCode之Best Time to Buy and Sell Stock III
- 修改Tomcat命令窗口的名字
- Ubuntu12.04 gedit中文文本乱码解决方法
- SpriteKit官方文档翻译和学习(一)
- 完美不代表成功
- 弹窗半透明
- leetcode_question_123 Best Time to Buy and Sell Stock III
- CODE 43: Decode Ways
- 伙伴算法
- 动态规划--子序列的个数
- 在JDK和Eclipse下Java Applet的编写和运行方式
- C语言static关键字的若干问题
- 修正IE下document.getElementsByName无法获取DIV标签
- 运算符重载函数作为类成员函数和友元函数
- VBox 问题: Failed to load VMMR0.r0 (VERR_SUPLIB_WORLD_WRITABLE).