Best Time to Buy and Sell Stock III 解题思路
题目要求:
最多交易两次,并且只能买卖完之后再买。
总思路:
在数组中找一个适当的点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 III 解题思路的更多相关文章
- LeetCode: Best Time to Buy and Sell Stock III 解题报告
Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...
- 【LeetCode】123. Best Time to Buy and Sell Stock III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- LeetCode 笔记23 Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- Best Time to Buy and Sell Stock | & || & III
Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【leetcode】123. Best Time to Buy and Sell Stock III
@requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...
- [leetcode]123. Best Time to Buy and Sell Stock III 最佳炒股时机之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- vmware产品
https://my.vmware.com/cn/group/vmware/info?slug=datacenter_cloud_infrastructure/vmware_vsphere/5_5#o ...
- 1 storm基本概念 + storm编程规范及demo编写
本博文的主要内容有 .Storm的单机模式安装 .Storm的分布式安装(3节点) .No space left on device .storm工程的eclipse的java编写 http:// ...
- Classical Inheritance in JavaScript
JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance in ...
- java第一章到第四章
class HelloWorld{ public static void main(String [] arguments) { System.out.println("Hello Worl ...
- Hibernate查询之HQL查询
转自:http://blog.csdn.net/xiao_yi/article/details/1733342 Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(H ...
- (7/18)重学Standford_iOS7开发_视图、绘制、手势识别_课程笔记
第七课: 1.View 一般来说,视图是一个构造块,代表屏幕上一块矩形区域,定义了一个坐标空间,并在其中绘制及添加触控事件等. ①视图的层级关系 一个视图只能有一个父视图,可以有多个子视图 - ( - ...
- eclipse package,source folder,folder区别及相互转换
今天遇到一个问题:在com.a.b.c这个包路径下建一个package,但是不知为什么就会自动编程folder,而且在这个“package”下的所有property文件读不到.所以看了一下文章:在ec ...
- 通过Navicat for MySQL远程连接的时候报错mysql 1130的解决方法
在用本地的navicat连接服务器的mysql数据库时候出现下面的问题: 解决的方法: 解决方法: 1.改表法.可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhos ...
- [WebGL入门]十四,绘制多边形
注意:文章翻译http://wgld.org/.原作者杉本雅広(doxas),文章中假设有我的额外说明,我会加上[lufy:].另外,鄙人webgl研究还不够深入.一些专业词语,假设翻译有误,欢迎大家 ...
- cocos2dx游戏项目总结(持续更新)
1.在一个layer里面,尽量只使用一种按钮类型.如MenuItem or CCControlButton.因为这两种按钮的优先级不同,在使用过程中会互相影响到事件触发的先后顺序. 2.细节的问题要一 ...