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 ...
随机推荐
- mysql忘记帐号密码 解决办法
首先关闭mysql 使用命令行启动mysql(一般要找到mysql.ini文件) 在windows上mysql.ini文件可以通过查看当前mysql进程参数查看到,具体方法点此 在启动mysql命令行 ...
- GNU风格 汇编语法总结(转)
转载自:http://blog.sina.com.cn/s/blog_78d30f6b0101713r.html 汇编源程序一般用于系统最基本的初始化:初始化堆栈指针.设置页表.操作 ARM的协处理器 ...
- HW4.30
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- android 使用intent传递参数实现乘法计算
主界面上是两个EditText和一个按钮.用于输入两个数字参数. calcute.xml: <?xml version="1.0" encoding="utf-8& ...
- 解决Ubuntu Server 12.04换了网卡MAC地址后 网络不可用的问题.
重装了系统,新建了一个ubuntu虚拟机,加载原来的镜像,结果启动后网络变得不正常了,提示信息 Waiting for network configuration...Waiting up to 60 ...
- PAT 1080. Graduate Admission (30)
It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applicat ...
- 20169210《Linux内核原理与分析》第五周作业
本次内容分为两部分,第一部分是实验,第二部分是教材的第四章和第六章. 第一部分:实验 本次的实验内容是构造一个简单的Linux系统MenuOS,过程如下. 首先使用如下命令进入LinuxKernel ...
- Android时间互换代码
收藏备用. http://www.oschina.net/code/snippet_575610_22694 /** * 获取现在时间 * * @return 返回时间类型 yyyy-MM-dd HH ...
- 【Qt】测测你对Qt的了解–Qt工具知多少
原文:http://blog.163.com/lijiji_1515/blog/static/1268774462009103101944455/ 你知道Qt都提供了哪些工具吗? 你知道Qt的 ...
- hadoop错误Ignoring exception during close for org.apache.hadoop.mapred.MapTask$NewOutputCollector@17bda0f2 java.io.IOException Spill failed
1.错误 Ignoring exception during close for org.apache.hadoop.mapred.MapTask$NewOutputCollector@17bd ...