将Best Time to Buy and Sell Stock的如下思路用到此题目

思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的。

思路2:第i天买出,能赚到的最大利润是多少呢?就是第i天的价格减去 0~ i-1天中最小的。

和前两道题比起来的话,这道题最难了,因为限制了交易次数。
解决问题的途径我想出来的是:既然最多只能完成两笔交易,而且交易之间没有重叠,那么就divide and conquer。
设i从0到n-1,那么针对每一个i,看看在prices的子序列[0,...,i][i,...,n-1]上分别取得的最大利润(第一题)即可。
这样初步一算,时间复杂度是O(n2)。

改进:
改进的方法就是动态规划了,那就是第一步扫描,先计算出子序列[0,...,i]中的最大利润,用一个数组保存下来,那么时间是O(n)。
第二步是逆向扫描,计算子序列[i,...,n-1]上的最大利润,这一步同时就能结合上一步的结果计算最终的最大利润了,这一步也是O(n)。
所以最后算法的复杂度就是O(n)的。

/*
解释:
首先,因为能买2次(第一次的卖可以和第二次的买在同一时间),但第二次的买不能在第一次的卖左边。
所以维护2个表,f1和f2,size都和prices一样大。
意义:
f1[i]表示 -- 截止到i下标为止,左边所做交易能够达到最大profit;[0,...,i]的利润
f2[i]表示 -- 截止到i下标为止,右边所做交易能够达到最大profit;[i,...,n-1]的利润
那么,对于f1 + f2,寻求最大即可。
*/

对于f1[i],求解过程中用price[i] 减去之前的最小值 和 f1[i-1]做比较,取最大值

动态规划转移方程 f1[i] = max(f1[i-1], price[i]- min)

对于f2[i],求解过程中用后面的最大值减去price[i]和f2[i+1]做比较,取最大值

动态规划转移方程 f2[i] = max(f2[i+1], max-price[i])

思路解释完毕,上code:

minX[i] 表示0 到 i 的最小值 的price

max[i] 表示i到n-1的最大值的price

 class Solution {
public:
int maxProfit(vector<int> &prices) {
if (prices.size() == )
return ; vector<int> f1(prices.size());
vector<int> f2(prices.size()); vector<int> minX(prices.size());
vector<int> maxX(prices.size()); minX[] = prices[];
for(int i = ; i< prices.size();i++ )
{
minX[i] = min(minX[i-], prices[i]);
} maxX[prices.size()-] = prices[prices.size()-];
for(int i = prices.size() -; i >=; i-- )
{
maxX[i] = max(maxX[i+], prices[i]);
} f1[] = ;
for(int i = ; i< prices.size();i++ )
{
f1[i] = max(f1[i-],prices[i]-minX[i]);
} f2[prices.size()-] = ;
for(int i = prices.size() -; i >=; i-- )
{
f2[i] = max(f2[i+],maxX[i]- prices[i]);
} int sum = ; for(int i = ; i< prices.size();i++ )
sum = max(sum, f1[i] + f2[i]); return sum; }
};

优化:可以对上述code稍微优化一下,maxX 和minX array 并不需要,只要保留一个变量mini和maxX即可,不过由于f1和f2 的存在,空间复杂度还是O(n)

 class Solution {
public:
int maxProfit(vector<int> &prices) {
if (prices.size() == )
return ; vector<int> f1(prices.size());
vector<int> f2(prices.size()); int mini = prices[];
f1[] = ;
for(int i = ; i< prices.size();i++ )
{
f1[i] = max(f1[i-],prices[i]-mini);
mini = min(mini, prices[i]);
} int maxi = prices[prices.size()-];
f2[prices.size()-] = ;
for(int i = prices.size() -; i >=; i-- )
{
f2[i] = max(f2[i+],maxi - prices[i]);
maxi = max(maxi, prices[i]);
} int sum = ; for(int i = ; i< prices.size();i++ )
sum = max(sum, f1[i] + f2[i]); return sum; }
};

[LeetCode] Best Time to Buy and Sell Stock III的更多相关文章

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

  2. [LeetCode] 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 ...

  3. LeetCode: Best Time to Buy and Sell Stock III [123]

    [称号] Say you have an array for which the ith element is the price of a given stock on day i. Design ...

  4. [Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机

    Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...

  5. [leetcode]Best Time to Buy and Sell Stock III @ Python

    原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意: Say you have an array ...

  6. leetcode -- Best Time to Buy and Sell Stock III TODO

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. LeetCode——Best Time to Buy and Sell Stock III

    Description: Say you have an array for which the ith element is the price of a given stock on day i. ...

  8. LeetCode——Best Time to Buy and Sell Stock III (股票买卖时机问题3)

    问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  9. LeetCode OJ--Best Time to Buy and Sell Stock III

    http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 这三道题,很好的进阶.1题简单处理,2题使用贪心,3题使用动态 ...

随机推荐

  1. U3D physics总结

    物理系统基于collider, 没有collider的物体不会发生任何主动和被动的物理交互,也不会产生trigger相关消息. 当且仅当A和B都有碰撞体时,两者才有可能发生交互,才有可能产生trigg ...

  2. C#中的Decimal类型

    这种类型又称财务类型,起源于有效数字问题.FLOAT 单精度,有效数字7位.有效数字是整数部分和小数部分加起来一共多少位.当使用科学计数法的,FLOAT型会出现很严重的错误.比如 8773234578 ...

  3. 妙味WEB前端开发全套视频教程+项目实战+移动端开发(99G)

    一共99GB的视频教程,全部存于百度网盘中,13个栏目,每个栏目里还划分有独立的小栏目 最基本的web前端学习介绍,到项目实战,再到移动端的开发,真正彻底掌握前端开发的精髓: 视频教程在线预览:(百度 ...

  4. MVC5 + EF6 + Bootstrap3 (8) HtmlHelper用法大全(上)

    文章来源:Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-httphelper-part1.html 上一节 ...

  5. 什么是co-training

    首先先认识下什么是co-training: 在计算机视觉中,我们都知道训练一个分类器的时候,我们需要两类样本,分别是正样本和负样本.监督训练又可以成为off-line training,就是提前准备好 ...

  6. 《android基于andFix的热修复方案》思路篇

    1:需求背景 项目上线之后,发现BUG需要修复(比如安卓兼容性等测试难以发现的问题),频繁的更新影响用户体验 2:方案要求 静默下载,耗费流量少,打完补丁后立刻生效,不用重启apk 3:解决思路 3. ...

  7. HTML5+NodeJs实现WebSocket即时通讯

    声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 最近都在学习HTML5,做canvas游戏之类的,发现HTML5中除了canvas这个强大的工具外,还有WebSocket也很值得注意.可 ...

  8. [BZOJ 1483][HNOI 2009]梦幻补丁(有序表启发式合并)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1483 分析: 先将不同的颜色的出现位置从小到大用几条链表串起来,然后统计一下答案 对于 ...

  9. [USACO2005][POJ2454]Jersey Politics(随机化)

    题目:http://poj.org/problem?id=2454 题意:给你3*k(k<=60)个数,你要将它们分成3个长度为k的序列,使得其中至少有两个序列的和大于k*500 分析:以为有高 ...

  10. [工具]推荐一款查看dll依赖工具

    引言 很久没写一篇像样的博客了,最近一个月一直忙于项目,也没时间去总结了,回到家,也就是看看书,没怎么总结.不过还是挺兴奋的,每天过得还算充实.这里也算是对五月份的一个总结吧. 为什么要查看dll 因 ...