Best Time to Buy and Sell Stock III 解题思路
2014-08-31 12:17
260 查看
题目要求:
最多交易两次,并且只能买卖完之后再买。
总思路:
在数组中找一个适当的点i,使得i左右两边profit之和最大。
思路:
1、从左往右扫描,left[i]记录包括i元素以内的左部的maxprofit,用Best Time to Buy and Sell Stock 1可得到。
2、从右往左扫描,right[i]记录i+1到尾部元素的maxprofit,将上面的low改成big即可
3、返回max(left[i],right[i])
注意:
1、考虑好,包括i与不包括i的情况
2、考虑好头尾,left[0]肯定是0,right[n-1]和right[n-2]也是0
最多交易两次,并且只能买卖完之后再买。
总思路:
在数组中找一个适当的点i,使得i左右两边profit之和最大。
思路:
1、从左往右扫描,left[i]记录包括i元素以内的左部的maxprofit,用Best Time to Buy and Sell Stock 1可得到。
2、从右往左扫描,right[i]记录i+1到尾部元素的maxprofit,将上面的low改成big即可
3、返回max(left[i],right[i])
注意:
1、考虑好,包括i与不包括i的情况
2、考虑好头尾,left[0]肯定是0,right[n-1]和right[n-2]也是0
相关文章推荐
- 解题思路:best time to buy and sell stock i && ii && iii
- [leetcode] 123. Best Time to Buy and Sell Stock III 解题报告
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告
- leecode 解题总结:123. Best Time to Buy and Sell Stock III
- LeetCode-Best Time to Buy and Sell Stock III -解题报告
- LeetCode - 123. Best Time to Buy and Sell Stock III - 思路详解 - C++
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告
- Leetcode 123. Best Time to Buy and Sell Stock III 股票买卖3 解题报告
- leetcode解题报告:123 Best Time to Buy and Sell Stock III
- Add to List 121. Best Time to Buy and Sell Stock,我的解题思路
- [Leetcode] 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 I II 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 I&&II&&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
- LeetCode - Best Time to Buy and Sell Stock III