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 ...
随机推荐
- 项目杂记(MONTHS_BETWEEN,Having ,Spool)
1,oracle中计算年龄: select FLOOR(MONTHS_BETWEEN(SYSDATE, to_date('20130728', 'yyyymmdd')) / 12), trunc(mo ...
- stl binary search
stl binary search */--> pre { background-color: #2f4f4f;line-height: 1.6; FONT: 10.5pt Consola,&q ...
- 《Differential Equations with Boundary-Value Problems》-chaper2-一阶线性方程
学习微分方程中,一个很常见的疑惑就是,我们所熟悉的非齐次微分方程的通解是对应齐次方程的通解加特解,但是更为重要的是,我们需要知道这句话是怎么得来的. 我们探讨一个未知问题的一般思路是将其不断的与已知已 ...
- Spark RDD/Core 编程 API入门系列之map、filter、textFile、cache、对Job输出结果进行升和降序、union、groupByKey、join、reduce、lookup(一)
1.以本地模式实战map和filter 2.以集群模式实战textFile和cache 3.对Job输出结果进行升和降序 4.union 5.groupByKey 6.join 7.reduce 8. ...
- Linux程序设计(一)入门
1. linux程序 Linux应用程序表现为两种特殊类型的文件:可执行文件和脚本文件. 可执行文件:是计算可以直接运行的程序,相当于windows的.exe文件. 脚本文件:一组指令的集合.这些指令 ...
- [T-SQL]从变量与数据类型说起
1.变量 学习计算机语言,难免不碰到这个名词,不过咱这里说的是MSSQL(微软SQL Server产品)里的数据库语言实现. 稍微对程序比较严谨的语言都要求使用之前都要声明变量先,比如c.c++,j ...
- 前端学习-使用JS库Leaflet.js生成世界地图并获取标注地址经纬度。
介绍:Leaflet是一个开源的JavaScript库,对移动端友好且对地图有很好的交互性. 大小仅仅只有 33 KB, 同时具有大多数地图所需要的特点. Leaflet设计的非常简单易懂, 同时具有 ...
- oracle Imp和exp以及导入常见的错误
一 1) 数据库对象已经存在 一般情况, 导入数据前应该彻底删除目标数据下的表, 序列, 函数/过程,触发器等; 数据库对象已经存在, 按缺省的imp参数, 则会导入失败 如果用了参数ignore=y ...
- find job
处于找工作的状态... 优秀求职者的5个问题: 1.在刚进来2-3个月里,你希望我可以完成哪些工作? 2.公司的优秀员工共同特征是什么? 3.业余时间,公司的员工一般在干什么?
- rnqoj-99-配置魔药-dp
比较好的题目~~ dp[j][k]: 第一个容器在第i秒和第二个容器在第j秒,所产生的最大魔力. if(num[i].t2<=j)dp[j][k]=max(dp[j][k],dp[num[i]. ...