123. Best Time to Buy and Sell Stock III
来源:互联网 发布:免费手机家谱制作软件 编辑:程序博客网 时间:2024/06/05 10:36
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).
题意:给定一个数组,第i个元素是第i天给定股票的价格。
设计一个算法来找到最大的利润。 您最多可以完成两个交易(2次买进2次卖出,2次交易不能有时间上的重叠)。
此题是前一题(前一题是最多1次交易)的升级版,我们可以参考前一题的思路来解决此题,附上连接:点击打开链接
我们知道了进行一次交易的解法,此题要求最多2次交易,那么我们最直接的想法就是将给定的数组分成2个数组,分别对这2个数组用上一题的解法求出最大利润值,二者相加即可得到我们想要的结果。
下面贴代码:
public class Solution { public int maxProfit(int[] prices) { int max = 0,max1 = 0; for(int i=1;i<prices.length-2;i++){ //将数组分成2部分,分别求最大利润 if(i==1 || prices[i]>prices[i-1]){ //优化条件 max1 = maxProfit1(prices,0,i+1)+maxProfit1(prices,i+1,prices.length); max = Math.max(max,max1); } } return Math.max(max,maxProfit1(prices,0,prices.length)); //考虑到1次交易可能比2次交易获得的利润更高 } public int maxProfit1(int[] prices,int start,int end) { //求数组从start到end中进行一次交易的最大利润 if(end-start<=1) return 0; int[] dp = new int[end-start-1]; dp[0] = prices[start+1]-prices[start]; for(int i=1;i<dp.length;i++){ if(dp[i-1]>=0) dp[i] = prices[start+i+1] - (prices[start+i]-dp[i-1]); else dp[i] = prices[start+i+1] - prices[start+i]; //System.out.println(dp[i]); } int max = 0; for(int k:dp){ if(k>max) max = k; } return max; }}相对于上一题,此题有几点需要注意的地方:
1.将maxProfit1方法加以改进,增加了起始位置和终止位置2个参数,避免了每次划分数组时需要申请的额外空间。
2.增加了优化条件。(大家可以试一下,如果去除了优化条件,在LeetCode上运行是会TLE的。)下面解释一下:
在对数组进行划分时,如果prices[i]<=prices[i-1],那么
对于数组的第一部分,第i天出售的利润不会高于第i-1天
对于数组的第二部分,每次划分都会减少一个元素,利润是不可能增加的
故此种情况下,总利润不可能增加,我们不需要进行计算。
- LeetCode 123. Best Time to Buy and Sell Stock III
- 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
- 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
- 123. Best Time to Buy and Sell Stock III
- [leetcode] 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- 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
- 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- [LeetCode] 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- Leetcode 123. Best Time to Buy and Sell Stock III
- PHPMailer 发邮件时 Extension missing: openssl 的解决方案
- 成都云栖大会_阿里1582.73亿营收背后的持续交付如何玩?
- cat命令
- Tolua使用笔记六:在lua中操作C#的委托事件与在lua中对Unity的GameObject的操作
- BZOJ4518: [Sdoi2016]征途
- 123. Best Time to Buy and Sell Stock III
- 让jenkins运行linux的后台命令
- path与classpath区别
- Android 屏幕适配方案
- $.Ajax()
- Fragment
- 中小企业怎么选择适合自己的网站建设公司_企业全网营销怎么做
- react native Animated 使用详解(基础)
- Thymeleaf 标准表达式语法