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

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

两次买卖之间是没有交叉的,即下一次买之前一定已经卖掉了。最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. .NET中常见的加解密方式

    在互联网普及的初期,人们更关注单纯的连接性,以不受任何限制地建立互联网为最终目的.正如事情都具有两面性,互联网的便捷性给人们带来了负面问题,计算机病毒的侵害.信息泄露.网络欺诈等利用互联网的犯罪行为日 ...

  2. 4,list,list的列表嵌套,range

    list 索引,切片+步长 # li = [, True, (, , , , , , '小明',], {'name':'alex'}] #索引,切片,步长 # print(li[]) # print( ...

  3. [转]python开发_shelve_完整版

    ''' python中的shelve模块,可以提供一些简单的数据操作 他和python中的dbm很相似. 区别如下: 都是以键值对的形式保存数据,不过在shelve模块中, key必须为字符串,而值可 ...

  4. 学习笔记7——wp版本更新需要注意的问题

    平时开发时应该避免修改wp的核心代码, 因为在升级wp版本时,核心代码都会被覆盖, wp升级时只有wp-content文件夹不会被覆盖.

  5. Educational Codeforces Round 31

    A. Book Reading time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  6. java第五章 子类与继承

    5.1子类与父类 1   java不支持多重继承,即一个子类不可以从多个父类中同时继承,而C++中可以. 人们习惯地称子类与父类的关系式“is—a”的关系 2   再类的声明过程中,通过关键字exte ...

  7. eclipse逆向生成实体类

    (转自:http://blog.csdn.net/wangpeng047/article/details/6877720) 做项目必然要先进行数据库表设计,然后根据数据库设计建立实体类(VO),这是理 ...

  8. scrapy框架之comand line tool

    一 Global Command 1 startproject https://docs.scrapy.org/en/latest/topics/commands.html#startproject ...

  9. vs2015插件推荐 Productivity Power Tools 2015

    vs2015插件推荐 Productivity Power Tools 2015 官网网址:https://marketplace.visualstudio.com/items?itemName=Vi ...

  10. hdu 1166 树状数组(线段树)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...