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 ...
随机推荐
- JavaScript高级程序设计58.pdf
15章 使用Canvas绘图 略 16章 HTML5脚本编程 HTML5规范了新的HTML标记和JavaScript API,以便简化创建动态Web界面的工作 跨文档消息传递 简称XDM,指来自不同域 ...
- No valid host was found
set in nova.conf -> scheduler_default_filters=AllHostsFilter Tried without success.
- __attribute__机制介绍 (转)
1. __attribute__ GNU C的一大特色(却不被初学者所知)就是__attribute__机制. __attribute__可以设置函数属性(Function Attribute).变量 ...
- Java SAX Schema Validation
It is possible to turn on XML Schema validation during parsing with a SAXParser. Here is how it look ...
- hihocoder 1077线段树
http://hihocoder.com/problemset/problem/1077 #include <bits/stdc++.h> using namespace std; #de ...
- Java多线程异步调度程序分析(二)
源自:http://blog.sina.com.cn/s/blog_4cc16fc50100c0uh.html public abstract class Result { //抽象的结果类 pu ...
- msvcp110.dll丢失
方法1:建议下载并安装[百度电脑专家],在搜索框内输入“vs2012运行时库缺失”,在搜索结果里面选择[立即修复],修复完成后验证是否正常: 方法2:手动修复 2.1 在[百度]下载“msvcp110 ...
- XML结构文件的读写
附件:http://files.cnblogs.com/xe2011/XML_Writer_And_Read.rar 下面这段代码实现了以下功能 数据保存 textBox1的文本,textBox2的文 ...
- Java基础知识强化之集合框架笔记62:Map集合之HashMap嵌套HashMap
1. HashMap嵌套HashMap 传智播客 jc 基础班 陈玉楼 20 高跃 ...
- sybase SA密码重置
sa 密码忘记解决之道: su - sybase cd ASE/install vi RUN_etoh2 在文件的末尾加入 -psa \ 停止原数据库服务 由于密码遗忘,所以只能通过kill进程停止服 ...