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


题解:DP

从前往后扫描一遍数组,用LeftToRight[i]记录(0,i)得到的最大利润;

从后往前扫描一遍数组,用RightToLeft[i]记录(i,n-1)得到的最大利润;

最终的最大利润是max(LeftToRight[i]+RightToLeft[i])。

举个例子,如果prices = {1,2,3,2,5,7},对应的

LeftToRight = {0,1,2,2,4,6}

RightToLeft = {6,5,5,5,2,0}

最终的最大利润就是2+5=7。表示在0~2(或0~3)这个区间取得利润2,并且在2~5(或者3~5)这个区间取得利润5,最终得到利润7.

代码如下:

 public class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length == 0)
return 0; int[] LeftToRight = new int[prices.length];
int[] RightToLeft = new int[prices.length]; int minimal = prices[0];
LeftToRight[0] = 0;
for(int i = 1;i < prices.length;i++){
minimal = Math.min(minimal, prices[i]);
LeftToRight[i] = Math.max(LeftToRight[i-1], prices[i]-minimal);
} int profit = 0;
//From Right to left
RightToLeft[prices.length-1] = 0;
int maximal = prices[prices.length-1];
for(int i = prices.length-2;i >= 0;i--){
maximal = Math.max(prices[i], maximal);
RightToLeft[i]= Math.max(RightToLeft[i+1], maximal-prices[i]);
profit = Math.max(profit, LeftToRight[i] + RightToLeft[i] );
} return Math.max(profit, LeftToRight[0]+RightToLeft[0]);
}
}

【leetcode刷题笔记】Best Time to Buy and Sell Stock III的更多相关文章

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

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

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

  4. LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV

    Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...

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

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

  7. LeetCode(122) Best Time to Buy and Sell Stock II

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

  8. Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)

    Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...

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

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

随机推荐

  1. billboard因为合批导致出问题的一个想法

    由于unity中距离较近的2个billboard物体会动态合批,如果缩放不同,显示就有问题.还得在shader中"DisableBatching"="true" ...

  2. Github上好的Android开源框架

    1.volley 项目地址 https://github.com/smanikandan14/Volley-demo (1)  JSON,图像等的异步下载: (2)  网络请求的排序(scheduli ...

  3. Java是否存在内存泄露

    会的. 原因:长生命周期的对象持有短生命周期对象的引用,导致短生命周期对象不能被回收,由此可能发生内存泄露. 举例参考:http://blog.csdn.net/yakihappy/article/d ...

  4. Python的Django框架中的Context使用

    Python的Django框架中的Context使用 近期整理些Python方面的知识,一旦你创建一个 Template 对象,你能够用 context 来传递数据给它. 一个context是一系列变 ...

  5. iOS表格制作

    由于项目上的需求,需要做一个表格出来,来显示流程状态.刚开始脑子一头雾水,没有一点思路,但是靠着自己的座右铭--“世上无难事,只怕有心人”,克服了所有困难.好,不说了,讲正事. 制作表格,还是需要ta ...

  6. C++模板类[初步]

    /* * stacktp.h * * Created on: 2014年3月29日 * Author: */ /** * - 模板类的概念,与使用 * -# export template <c ...

  7. 九度OJ 1347:孤岛连通工程 (最小生成树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1522 解决:314 题目描述: 现在有孤岛n个,孤岛从1开始标序一直到n,有道路m条(道路是双向的,如果有多条道路连通岛屿i,j则选择最短 ...

  8. 低秩近似 low-rank approximation

  9. SAP ATP邏輯可用性檢查

    [转http://tqmeng.blog.163.com/blog/static/169263916201162002414612/]SAP ATP邏輯可用性檢查1.可用性檢查群組OVZ2主要用於檢查 ...

  10. MySQL一些常见查询方式

    1.查询端口号命令: show global variables like 'port'; 2.查看版本号: select version(); 3.查看默认安装的MySQL的字符集 show var ...