将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. Angular权威指南学习笔记

    第一章.        初识Angular--Angular是MVW的Js框架. 第二章.        数据绑定--ViewModel中不仅可以含有变量,还可以还有事件.可以通过事件来控制变量的值改 ...

  2. linux内核分析 第4章读书笔记

    第四章 进程调度 一.抢占与非抢占 1.非抢占式进程调度 进程会一直执行直到自己主动停止运行 2.抢占式进程调度 Linux/Unix使用的是抢占式的方式,强制的挂起进程的动作就叫做抢占. 二.进程优 ...

  3. RocEDU.阅读.写作《图解TCP/IP》

    2015年11月21日 一.对本书的认识 信息通信社会这个词俨然是现代社会的一个名词.人们可以使用各种信息终端随时随地的进行交流,而这种环境正是以来网络才得以实现,使用最为广泛的协议就是TCP/IP协 ...

  4. 魅蓝Note2 在Android Studio 与 Eclipse中无法被检测到

    昨天到手的Note2 结果发现测试不了,一看魅蓝的版本是android 5.1,然后更新的自己的SDK. 最后…… 仍然不能识别到手机. ———————————— 今天在stackoverflow上搜 ...

  5. protobuf 文件级别优化

    package IM.BaseDefine;option java_package = "com.mogujie.tt.protobuf";option optimize_for ...

  6. [codevs1283]等差子序列(二进制)

    题目:http://codevs.cn/problem/1283/ 分析: 主要就是在每个判定上节省时间.一般的做法是开个数组记录每个数字出没出现,然后每次读入一个数字就以他为中间向两边扩展直到两个对 ...

  7. Spring security 学习 (自助者,天助之!)

    自己努力,何必要强颜欢笑的求助别人呢?  手心向下不求人! Spring security学习有进展哦: 哈哈! 1.页面都是动态生产的吧! 2.设置权限:  a:pom.xml配置jar包 b:cr ...

  8. asp.net 捕获全局未处理异常的几种方法

    通过HttpModule来捕获未处理的异常[推荐] 首先需要定义一个HttpModule,并监听未处理异常,代码如下: public void Init(HttpApplication context ...

  9. 【BZOJ 3188】【Coci 2011】Upit Splay模板题

    转啊转终于转出来了,然而我的模板跟陈竞潇学长的模板一模一样,还是太弱啊,第一次用指针. #include<cstdio> #include<cstring> #include& ...

  10. 复习CSS

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...