Best Time to Buy and Sell Stock III 解答
Question
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).
Solution
This problem can be solved by "divide and conquer". We can use left[i] array to track maximum profit for transactions before i (including i), and right[i + 1] to track maximum profit for transcations after i.
Prices: 1 4 5 7 6 3 2 9
left = [0, 3, 4, 6, 6, 6, 6, 8]
right= [8, 7, 7, 7, 7, 7, 7, 0]
Time complexity O(n), space cost O(n).
public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2)
return 0;
int length = prices.length, min = prices[0], max = prices[length - 1], tmpProfit = 0;
int[] leftProfits = new int[length];
leftProfits[0] = 0;
int[] rightProfits = new int[length];
rightProfits[length - 1] = 0;
// Calculat left side profits
for (int i = 1; i < length; i++) {
if (prices[i] > min)
tmpProfit = Math.max(tmpProfit, prices[i] - min);
else
min = prices[i];
leftProfits[i] = tmpProfit;
}
// Calculate right side profits
tmpProfit = 0;
for (int j = length - 2; j >= 0; j--) {
if (prices[j] < max)
tmpProfit = Math.max(tmpProfit, max - prices[j]);
else
max = prices[j];
rightProfits[j] = tmpProfit;
}
// Sum up
int result = Integer.MIN_VALUE;
for (int i = 0; i < length - 1; i++)
result = Math.max(result, leftProfits[i] + rightProfits[i + 1]);
result = Math.max(result, leftProfits[length - 1]);
return result;
}
}
Best Time to Buy and Sell Stock III 解答的更多相关文章
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- LeetCode 笔记23 Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- Best Time to Buy and Sell Stock | & || & III
Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【leetcode】123. Best Time to Buy and Sell Stock III
@requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...
- 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 ...
- [leetcode]123. 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 ...
- LN : leetcode 123 Best Time to Buy and Sell Stock III
lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...
随机推荐
- Rhythmbox中文乱码解决的方法
转自:http://hi.baidu.com/morgensonne/item/3470aef58747abde6325d2d9 今天在网络上找到了一个比較好的解决Rhythmbox中文乱码的问题的方 ...
- Android窗口管理服务WindowManagerService对壁纸窗口(Wallpaper Window)的管理分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8550820 Android系统中,壁纸窗口和输 ...
- 一道movfuscator混淆过的简单逆向
月赛中出了道经过movfuscator混淆的逆向题目,记录一下过程.跑起来发现需要用户输入长度为20的字符串,我尝试着输入了几次都是直接退出了,没有任何提示.用IDA打开,题目里面几乎全是mo ...
- NET基础课--泛型(NET之美)
1.泛型,类型或方法的一种抽象概括. 2.泛型类:在类型名后面加一个<>,其中传递占位符,也就是类型参数.where是类型约束 可以再查资料 public class SortHelper ...
- StackOverflow程序员推荐:每个程序员都应读的30本书
“如果能时光倒流,回到过去,作为一个开发人员,你可以告诉自己在职业生涯初期应该读一本,你会选择哪本书呢?我希望这个书单列表内容丰富,可以涵盖很多东西.” 很多程序员响应,他们在推荐时也写下自己的评语. ...
- 如何在WP8模拟器中连接本地的web服务
这个问题困扰了很久,查找答案一度找偏方向. 其实连接web服务对于wp7不是问题,因为wp7使用的网络就是本机的网络,但是到了wp8模拟器,纯粹的虚拟机,独立的设备,也就有了自己的网络连接,要当做虚拟 ...
- 疯狂安卓Android自学笔记
开发者必备自学工具: 谷歌搜索:www.yundou.info Android原版开发文档 (英文) Doc http://www.phoned.cn/docs/reference/android/v ...
- .NET的 DataTable中某列求和
public DataTable ReportDetail { get; set; }//定义datatable属性 this.txtTotalPiece.Text = ReportDetail.Co ...
- .Net Memory -- Windbg基本命令
命令 解释 .cls 清空命令窗口屏幕 .load dllfullpath 加载debugger扩展dll如SOS sosex psscor. .loadby dll moduleName 加载deb ...
- UVA 719 / POJ 1509 Glass Beads (最小表示法/后缀自动机)
题目大意: 给出一个长度为N的字符串,求其字典序最小的循环同构. N<=10W. 算法讨论: 算法一.最小表示法.定义题. 算法二.后缀自动机. Codes: #include <iost ...