leetcode第一刷_Best Time to Buy and Sell Stock III
这道题还是挺难的,属于我前面提到的,给个数组,线性时间找出个什么东西,尽管上面的两个买卖股票也是这类。只是相比之下稚嫩多了。有关至少至多的问题比較烦人,不好想,等再做一些题,可能会发现什么规律。这道题的情况还是比較少的,要么买卖了两次。要么一次。
买卖一次的情况,已经解决过了,如今分析买卖两次的情况。
两次买卖之间是没有交叉的,即下一次买之前一定已经卖掉了。最easy想到,穷去分点,每一个部分都依照买卖一次的方法做。
好,恭喜。你已经走在超时的路上了。那么怎么办呢。有没有一种方法,在线性时间内,等我走到一个分点的时候,就能知道划分两次求出来的结果?
没错。辅助空间。先从头往后扫一遍,记录下以这个点为终点时的最大收入,然后从尾向头扫一遍。记录下以这个点为起点的最大收入。
再对每一个分点扫一遍,看看已他为起点终点算出来的交易收入是不是最优的。
思路是这种。描写叙述起来比較清晰。实现的时候还是有能够优化的地方的,比如你发现从尾向头扫描的结果根本不用一整个数组。仅仅要保存好后一个位置的最优值就能够了。由于当前最优值仅仅跟峰值与当前值以及过去最优相关。
代码例如以下。未优化空间:
class Solution {
public:
int maxProfit(vector<int> &prices) {
int len = prices.size();
if(len <= 1)
return 0;
vector<int> phistory(len);
vector<int> pfuture(len);
int valley = prices[0], peak = prices[len-1];
for(int i=1;i<len;i++){
valley = min(valley, prices[i]);
phistory[i] = max(phistory[i-1], prices[i]-valley);
}
int res = 0;
for(int i=len-2;i>=0;i--){
peak = max(peak, prices[i]);
pfuture[i] = max(pfuture[i+1], peak-prices[i]);
res = max(res, pfuture[i]+phistory[i]);
}
return res;
}
};
leetcode第一刷_Best Time to Buy and Sell Stock III的更多相关文章
- leetcode第一刷_Best Time to Buy and Sell Stock II
这道题尽管是上一道题的增强.可是反而简单了. 能够交易无数次,可是买卖必须成对的出现. 为了简单起见.我用abc三股股票来说明,且忽略掉相等的情况.三个数一共同拥有六种大小关系.注意他们之间的先后顺序 ...
- leetcode第一刷_Best Time to Buy and Sell Stock
这样的题就不要去考虑N^2的算法了.肯定会超时的.乍一看,非常可能会想到贪心,可是普通的贪心思路是不行的,比方想找到一个最小值用来买入.尽管它跟最大值之间的差一定是最好的,可是最大值出如今它前面就不行 ...
- 【刷题-LeetCode】123 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 ...
- 【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 笔记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 ...
- 【LeetCode OJ】Best Time to Buy and Sell Stock III
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ Linear Time Solut ...
- LeetCode OJ 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 ...
- 【LeetCode】123. Best Time to Buy and Sell Stock III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【刷题-LeetCode】188 Best Time to Buy and Sell Stock IV
Best Time to Buy and Sell Stock IV Say you have an array for which the i-th element is the price of ...
随机推荐
- Python Flask+Mysql练习题
#!/usr/bin/pythonfrom flask import Flask,render_template,request,redirect,sessionimport MySQLdb as m ...
- chrome 下载插件包及离线安装 附 Advanced Rest Client 下载
最近需要测试http rest服务,由于chrome插件的轻便,首先想到了用chrome插件,在google商店找到Advanced Rest Client,用了一阵感觉不错. 于是项目组其他同事也要 ...
- springboot集成redis操作
使用HashOperations操作redis----https://www.cnblogs.com/shiguotao-com/p/10560458.html 使用HashOperations操作r ...
- [android 应用框架api篇] bluetooth
bluetooth接口 android.bluetooth.jar 官网网址: 接口类: https://developer.android.com/reference/android/bluetoo ...
- Android单个按钮自定义Dialog
代码改变世界 Android单个按钮自定义Dialog dialog_layout.xml <?xml version="1.0" encoding="utf-8& ...
- VM上完美运行macos
VM上完美运行macos 作者:方辰昱 时间:十月三号 效果图 简要步骤 下载安装VM 下载镜像文件链接,darwin.iso,unlocker,beamoff.合集下载链接:https://pan. ...
- BZOJ 1419 Red is good ——期望DP
定义f[i][j]表示还剩i张红牌,j张黑牌的时候能取得的期望最大值 显然有$f[i][j]=max(0,\frac {i}{i+j}(f[i-1][j]+1)+ \frac {j}{i+j}(f[i ...
- BZOJ 3227 [Sdoi2008]红黑树(tree) ——贪心 动态规划
首先可以想到一个贪心的方法,然后一层一层的合并. 也可以采用动态规划的方式,为了写起来好写,把点数*2+1,然后发现在本机上跑不过1500的数据. 交上去居然A掉了. 贪心 #include < ...
- Mybatis 如何自动生成bean dao xml 配置文件 generatorconfig.xml (main()方法自动生成更快捷)
最近项目要用到mybatis中间件,中间涉及到要对表结构生成bean,dao,和sqlconfig.xml 所以记录一下学习过程 首先是准备工作,即准备需要的jar包:我们的数据库mysql,所以驱动 ...
- Java Interface 是常量存放的最佳地点吗?(转帖学习,非原创)
Java Interface 是常量存放的最佳地点吗?(转帖学习,非原创) 由于java interface中声明的字段在编译时会自动加上static final的修饰符,即声明为常量.因而inter ...