将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. C语言 百炼成钢4

    //题目10:打印楼梯,同时在楼梯上方打印两个笑脸. #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdli ...

  2. DWZ (JUI) 教程 navTab 刷新分析

    navTab的刷新在doc文件里也有说明 首先 在form表单里指定好回调函数 * <form action="/user.do?method=save" onsubmit= ...

  3. Bootstrap 表格

    Bootstrap 提供了一个清晰的创建表格的布局.下表列出了 Bootstrap 支持的一些表格元素: 标签 描述 <table> 为表格添加基础样式. <thead> 表格 ...

  4. IOS 应用生命周期

    *当第一次运行程序时候:(active)didFinishLaunchingWithOptions(加载完毕)->applicationDidBecomeActive(获取焦点)*当点击home ...

  5. Python积木之with

    简而言之,with 语句是典型的程序块 “try catch finally”的一种模式抽取.python的作者在PEP343中写道 “ This PEP adds a new statement & ...

  6. Silverlight button 图片切换样式

    之前一直做WPF现在开始接触Slilverlight感触很多. 今天做一个Button要求 有两个图片,button默认有一个图片,鼠标over时用另一个图片, 用wpf做的时候写一个template ...

  7. session放到mongo里边

    为了session的共享和保存,常常把session放数据库里边,但是很多时候都放redis里边,今天看了一个放Mongo中的记录下,说不懂什么时候就用到了. app.use(session({ se ...

  8. Zebra_Dialog 弹出层插件

    . Default dialog box, no custom settings. Click here to open. $.Zebra_Dialog('<strong>Zebra_Di ...

  9. BootStrap网格布局

    如何使用BootStrap样式 BootStrap与其他的开源库类似,直接引用它的css样式文件就可以使用了. <link rel="stylesheet" href=&qu ...

  10. if转换switch的小技巧

    class Program { static void Main(string[] args) { /* 对学员的结业考试成绩评测(用switch) * 成绩>=90……A * 90>成绩 ...