这道题还是挺难的,属于我前面提到的,给个数组,线性时间找出个什么东西,尽管上面的两个买卖股票也是这类。只是相比之下稚嫩多了。有关至少至多的问题比較烦人,不好想,等再做一些题,可能会发现什么规律。这道题的情况还是比較少的,要么买卖了两次。要么一次。

买卖一次的情况,已经解决过了,如今分析买卖两次的情况。

两次买卖之间是没有交叉的,即下一次买之前一定已经卖掉了。最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的更多相关文章

  1. leetcode第一刷_Best Time to Buy and Sell Stock II

    这道题尽管是上一道题的增强.可是反而简单了. 能够交易无数次,可是买卖必须成对的出现. 为了简单起见.我用abc三股股票来说明,且忽略掉相等的情况.三个数一共同拥有六种大小关系.注意他们之间的先后顺序 ...

  2. leetcode第一刷_Best Time to Buy and Sell Stock

    这样的题就不要去考虑N^2的算法了.肯定会超时的.乍一看,非常可能会想到贪心,可是普通的贪心思路是不行的,比方想找到一个最小值用来买入.尽管它跟最大值之间的差一定是最好的,可是最大值出如今它前面就不行 ...

  3. 【刷题-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 ...

  4. 【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 ...

  5. 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 ...

  6. 【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 ...

  7. 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 ...

  8. 【LeetCode】123. Best Time to Buy and Sell Stock III 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【刷题-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 ...

随机推荐

  1. 关于php使用xpath解析html中文乱码问题

    $str2 = '<div id="content">我很好 </div>'; $dom = new DOMDocument(); //load之前强转字符 ...

  2. 关于学习Mongodb的几篇文章

    一.Mongodb分片的使用 http://www.caiyiting.com/blog/2014/mongodb-sharding.html 二.MongoDB分布式高可用集群实现 http://w ...

  3. adb 命令大全

    传送门 --> https://github.com/mzlogin/awesome-adb ADB,即 Android Debug Bridge,它是 Android 开发/测试人员不可替代的 ...

  4. PYDay3-初识python

    Python 种类 c.j.iron.ruby等,主要有三类:cpython.xxxpython.pypy 种类繁多我们精通一种即可 编译流程: py代码->字节码->机器码->计算 ...

  5. POJ-2318 TOYS,暴力+叉积判断!

                                                                 TOYS 2页的提交记录终于搞明白了. 题意:一个盒子由n块挡板分成n+1块区 ...

  6. HDU——1242Rescue(BFS+优先队列求点图最短路)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  7. BZOJ 4824 [Cqoi2017]老C的键盘 ——树形DP

    每一个限制条件相当于一条有向边, 忽略边的方向,就成了一道裸的树形DP题 同BZOJ3167 唯一的区别就是这个$O(n^3)$能过 #include <map> #include < ...

  8. 刷题总结——road(ssoi)

    题目: 题目背景 SOURCE:NOIP2016-RZZ-1 题目描述 有 N 个城市,这些城市通过 M 条无向边互相连通,每条边有一个权值 Ci ,表示这条边的长度为 2^(Ci) ,没有两条边的长 ...

  9. hdu5396 Expression

    Expression Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  10. C++ assert 的一点说明

    断言(ASSERT)的用法 转载自http://www.cnblogs.com/moondark/archive/2012/03/12/2392315.html 我一直以为assert仅仅是个报错函数 ...