LeetCode: Best Time to Buy and Sell Stock III 解题报告
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;
}
LeetCode: Best Time to Buy and Sell Stock III 解题报告的更多相关文章
- 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 ...
- 【LeetCode】123. Best Time to Buy and Sell Stock III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- [LeetCode] Best Time to Buy and Sell Stock III
将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...
- 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 ...
- [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 ...
- Best Time to Buy and Sell Stock III 解题思路
题目要求: 最多交易两次,并且只能买卖完之后再买. 总思路: 在数组中找一个适当的点i,使得i左右两边profit之和最大. 思路: 1.从左往右扫描,left[i]记录包括i元素以内的左部的maxp ...
- [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 ...
随机推荐
- js算法初窥05(算法模式02-动态规划与贪心算法)
在前面的文章中(js算法初窥02(排序算法02-归并.快速以及堆排)我们学习了如何用分治法来实现归并排序,那么动态规划跟分治法有点类似,但是分治法是把问题分解成互相独立的子问题,最后组合它们的结果,而 ...
- <<c专家编程>>笔记
C专家编程摘录 c操作符的优先级 有时一些c操作符有时并不会像你想象的那样工作. 下方表格将说明这个问题: 优先级问题 表达式 期望的情况 实际情况 . 优先级高于* *p.f (*p).f *(p. ...
- 7,EasyNetQ-控制队列名称
EasyNetQ在为队列生成名称时的默认行为是使用 消息类型名称+subscription Id 例如,名称空间EasyNetQ.Tests.Integration中的PartyInvitatio ...
- 蓝牙扫描工具btscanner修复暴力扫描模式
蓝牙扫描工具btscanner修复暴力扫描模式 在btscanner 2.1-5版本中,当用户按下快捷键b,执行暴力扫描模式,会出现程序奔溃问题.该问题现在已经修复.用户只需要更新系统,将btsc ...
- pho文件操作
php文件操作函数 readfile - 适用于打开一个文件并读取文件的内容 echo readfile('e:/webdictionary.txt'); fopen('文件名','打开模式') - ...
- react-native基础教程(1)
转载链接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/react-native-foundation-course/ React ...
- Windows2003配置集群详解
原文: http://blog.csdn.net/xunyn/article/details/7388900 集群是在一组计算机上运行相同的软件并虚拟成一台主机系统为客户端与应用提供服务:计算机通过缆 ...
- Servlet第五课:Cookie的使用
目标规划: 通过这一节课,我们能够懂得怎样使用Cookie.以及怎样获取Cookie中的内容. 插播广告:博客之星评选.点击投我一票.谢谢. Cookie的具体概述. 1. Cookie 是保存在cl ...
- CustomJsonDateDeserializer @JsonDeserialize(using = CustomJsonDateDeserializer.class) Jackson 反序列化Date时遇到的问题 java中json日期属性反序列化
public class CustomJsonDateDeserializer extends JsonDeserializer<Date> { @Override public Date ...
- HOW TO REPLACE ALL OCCURRENCES OF A CHARACTER IN A STD::STRING
From: http://www.martinbroadhurst.com/replacing-all-occurrences-of-a-character-in-a-stdstring.html T ...