Best Time to Buy and Sell Stock III
Question Solution
Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most two transactions.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

解答:
1. 从左往右扫描,计算0-i的这个区间的最大利润。方法可以参见股票第一题
2. 从右往左扫描,计算i-len这个区间的最大利润。方法同上。
3. 再从头至尾扫一次,每个节点加上左边和右边的利润。记录最大值。

复制一点别人的讲解:

O(n^2)的算法很容易想到:

找寻一个点j,将原来的price[0..n-1]分割为price[0..j]和price[j..n-1],分别求两段的最大profit。

进行优化:

对于点j+1,求price[0..j+1]的最大profit时,很多工作是重复的,在求price[0..j]的最大profit中已经做过了。

类似于Best Time to Buy and Sell Stock,可以在O(1)的时间从price[0..j]推出price[0..j+1]的最大profit。

但是如何从price[j..n-1]推出price[j+1..n-1]?反过来思考,我们可以用O(1)的时间由price[j+1..n-1]推出price[j..n-1]。

最终算法:

数组l[i]记录了price[0..i]的最大profit,

数组r[i]记录了price[i..n]的最大profit。

已知l[i],求l[i+1]是简单的,同样已知r[i],求r[i-1]也很容易。

最后,我们再用O(n)的时间找出最大的l[i]+r[i],即为题目所求。(最后一步可以合并在第二步中)。

REF: http://blog.csdn.net/pickless/article/details/12034365

代码1:

 public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
} int len = prices.length;
int[] left = new int[len];
int[] right = new int[len]; int min = prices[0];
left[0] = 0;
for (int i = 1; i < len; i++) {
min = Math.min(min, prices[i]);
left[i] = Math.max(left[i - 1], prices[i] - min);
} int max = prices[len - 1];
right[len - 1] = 0;
for (int i = len - 2; i >= 0; i--) {
max = Math.max(max, prices[i]);
right[i] = Math.max(right[i + 1], max - prices[i]);
} int rst = 0;
for (int i = 0; i < len; i++) {
rst = Math.max(rst, left[i] + right[i]);
} return rst;
}

代码2:

 public class Solution {
public int maxProfit(int[] prices) {
if (prices == null) {
return 0;
} int ret = 0; int len = prices.length;
int[] leftProfile = new int[len];
int profile = 0; int min = Integer.MAX_VALUE;
for (int i = 0; i < len; i++) {
min = Math.min(min, prices[i]);
profile = Math.max(profile, prices[i] - min);
leftProfile[i] = profile;
} int max = Integer.MIN_VALUE;
profile = 0;
for (int i = len - 1; i >= 0; i--) {
max = Math.max(max, prices[i]);
profile = Math.max(profile, max - prices[i]); // sum the left profit and the right profit.
ret = Math.max(ret, profile + leftProfile[i]);
} return ret;
}
}

DP思路:

 // DP solution:
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
} int ret = 0; int len = prices.length;
int[] leftProfile = new int[len]; int min = prices[0];
leftProfile[0] = 0;
for (int i = 1; i < len; i++) {
min = Math.min(min, prices[i]);
leftProfile[i] = Math.max(leftProfile[i - 1], prices[i] - min);
} int max = Integer.MIN_VALUE;
int profile = 0;
for (int i = len - 1; i >= 0; i--) {
max = Math.max(max, prices[i]);
profile = Math.max(profile, max - prices[i]); // sum the left profit and the right profit.
ret = Math.max(ret, profile + leftProfile[i]);
} return ret;
}

GitHub代码链接

LeetCode: Best Time to Buy and Sell Stock III 解题报告的更多相关文章

  1. LeetCode: Best Time to Buy and Sell Stock II 解题报告

    Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...

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

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

  3. 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)

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

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

  5. [LeetCode] Best Time to Buy and Sell Stock III

    将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...

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

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

  8. Best Time to Buy and Sell Stock III 解题思路

    题目要求: 最多交易两次,并且只能买卖完之后再买. 总思路: 在数组中找一个适当的点i,使得i左右两边profit之和最大. 思路: 1.从左往右扫描,left[i]记录包括i元素以内的左部的maxp ...

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

随机推荐

  1. Linux学习之用户配置文件详解(十四)

    Linux学习之用户配置文件详解 目录 用户信息文件/etc/password 影子文件/etc/shadow 组信息文件/etc/group 组密码文件/etc/gshadow 用户信息文件/etc ...

  2. epoll使用详解

    目录 epoll介绍 Epoll的优点: 1.支持一个进程打开大数目的socket描述符(FD) 2.IO效率不随FD数目增加而线性下降 3.支持边缘触发模式 4.使用mmap加速内核与用户空间的消息 ...

  3. UC浏览器中Ajax请求中传递数据的一个坑

    今天突然收到一个bug,有用户在其浏览器环境中一直无法提交内容,使用的是UC浏览器.当换成Chrome时,内容能够正常提交.鉴于本地没有一直使用Firefox 以及Chrome,于是去下载了一个UC ...

  4. 开启mysql的远程访问权限

    改表法 1.登陆mysql mysql -u root -p 2.修改mysql库的user表,将host项,从localhost改为%.%这里表示的是允许任意host访问,如果只允许某一个ip访问, ...

  5. [软件研究]对AMH面板的研究

    0x00 前言 继续研究,这次来看一下AMH面板,图截自官网 就让我们来看看这个多个"首个"的面板做的怎么样吧. 0x01 安装 官方提供了两种安装方式,一是极速安装,二是编译安装 ...

  6. 洛谷.1782.旅行商的背包(背包DP 单调队列)

    题目链接(卡常背包) 朴素的多重背包是: \(f[i][j] = \max\{ f[i-1][j-k*v[i]]+k*w[i] \}\),复杂度 \(O(nV*\sum num_i)\) 可以发现求\ ...

  7. C++ 模板应用举例_模板实现STL类(堆栈)

    //stack集合类是一个简单的堆栈的实现. //这里有两个模板参数,T和size,指定堆栈中的元素类型和堆栈中项数的最大值. //push 和 pop成员函数添加和删除堆栈中的项,并在堆栈底部增加. ...

  8. 【NOIP2018】【RP++!】【神大退役记+一丢丢回忆录】

    emmm初赛都完了啊,还有20多天的样子退役选手又要++++++了 所以在这里先预祝各路dalao取得好成绩!! 手动艾特亲友$@Abyssful@阿澈说他也想好好学习@Ed\_Sheeran@歪瓜是 ...

  9. css中display:none与visibility: hidden的区别

    display: none; 隐藏元素,不占用其本来空间------对应元素显示用的是display:block; visibility: hidden:元素隐藏,占用其本来的页面空间-------- ...

  10. Voltage Translation for Analog to Digital Interface ADC

    Voltage Translation for Analog to Digital Interface 孕龙逻辑分析仪 ZeroPlus Logic Analyzer How to modify an ...