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


才意识到能够在整个区间的每一点切开,然后分别计算左子区间和右子区间的最大值,然后再用O(n)时间找到整个区间的最大值。

看来以后碰到与2相关的问题,一定要想想能不能用二分法来做!
 
以下复制pickless的解说,我认为我不能比他讲的更好了
O(n^2)的算法非常easy想到:
找寻一个点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]也非常easy。
最后,我们再用O(n)的时间找出最大的l[i]+r[i],即为题目所求。

package Level4;  

import java.util.Arrays;  

/**
* 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 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).
*
*/
public class S123 { public static void main(String[] args) {
// int[] prices = {3,3,5,0,0,3,1,4};
int[] prices = {2,1,2,0,1};
System.out.println(maxProfit(prices));
} // 基本思想是分成两个时间段,然后对于某一天,计算之前的最大值和之后的最大值
public static int maxProfit(int[] prices) {
if(prices.length == 0){
return 0;
} int max = 0;
// dp数组保存左边和右边的利润最大值
int[] left = new int[prices.length]; // 计算[0,i]区间的最大值
int[] right = new int[prices.length]; // 计算[i,len-1]区间的最大值 process(prices, left, right); // O(n)找到最大值
for(int i=0; i<prices.length; i++){
max = Math.max(max, left[i]+right[i]);
} return max;
} public static void process(int[] prices, int[] left, int[] right){
left[0] = 0;
int min = prices[0]; // 左边递推公式
for(int i=1; i<left.length; i++){
left[i] = left[i - 1] > prices[i] - min ? left[i - 1] : prices[i] - min;
min = prices[i] < min ? prices[i] : min;
} right[right.length-1] = 0;
int max = prices[right.length-1];
// 右边递推公式
for(int i=right.length-2; i>=0; i--){
right[i] = right[i + 1] > max - prices[i] ? right[i + 1] : max - prices[i];
max = prices[i] > max ? prices[i] : max;
} // System.out.println(Arrays.toString(left));
// System.out.println(Arrays.toString(right));
} }

LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法的更多相关文章

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

  2. 【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 ...

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

  4. 【刷题-LeetCode】123 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 ...

  5. 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 a ...

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

  7. 123. Best Time to Buy and Sell Stock III ——LeetCode

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. 123. Best Time to Buy and Sell Stock III (Array; DP)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. 有关于web server架构的一个小疑问

    今天闲的时候trace route了yahoo和sina的域名,yahoo的如下: 1     1 ms     1 ms    <1 ms  172.21.127.1   2     3 ms ...

  2. Cocos2d-x 游戏存档

    游戏存档功能能够保存游戏中数据.让玩家游戏能够延续. 单机游戏更为重要.而CCUserDefault能够作轻量级数据库使用,用来存储数据,支持数据类型bool,int, float, double, ...

  3. VMware vSphere 服务器虚拟化之二十八 桌面虚拟化之安装View传输服务器

    VMware vSphere 服务器虚拟化之二十八 桌面虚拟化之安装View传输服务器 View 传输服务器用于管理和简化数据中心与在最终用户本地系统上检出使用的 View 桌面之间的数据传输.必须安 ...

  4. virtenv 0.8.6 发布,虚拟桌面配置工具 - 开源中国社区

    virtenv 0.8.6 发布,虚拟桌面配置工具 - 开源中国社区 virtenv 0.8.6 发布,virtenv 是一个用 QT4 开发的应用,用来配置和启动基于 LXC 的虚拟桌面环境.该容器 ...

  5. 1T文件夹 - 微云

    1T文件夹 - 微云 1T文件夹

  6. HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............

    看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. ...

  7. oracle 之 内存—鞭辟近里(二)

    overview of the pga pga是在操作系统的进程或是线程特定的一块内存区域,它不是共享的.因为pga是进程指定的,因此它不会在sga中分配. pga是一个内存堆,其中包含了被专用服务器 ...

  8. 黑马程序猿————OC在Foundation框架结构和字符串

    ------<a href="http://www.itheima.com" target="blank">Java火车.Android火车.iOS ...

  9. deinstall oracle 11g on linux

    deinstall oracle 11g on linux   From 11gR2, oracle provide us an deinstall tool. With that now we ca ...

  10. 使用 SQLNET.EXPIRE_TIME 清除僵死连接

    数据库连接的客户端异常断开后,其占有的相应并没有被释放,如从v$session视图中依旧可以看到对应的session处于inactive,且对应的服务器进程也没有释放,导致资源长时间地被占用,对于这种 ...