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. M - 基础DP

    M - 基础DP Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descriptio ...

  2. vue Element UI 导航高亮

    1. activeIndex 为默认高亮值,根据改变activeIndex的值来改变高亮的值 当页面改变的时候获取当前的路由地址,截取第一个 / 后面的值,就是当前的高亮值了 为什么要截取呢? 因为点 ...

  3. 第三课 nodejs读取文件

    //引入文件操作模块var fs = require('fs'); //读取文件 使用 回调函数 utf-8编码读取 a.txt在当前文件目录fs.readFile('a.txt','UTF-8',f ...

  4. 第一课 第一个nodejs程序

    这就是我们的第一个程序了 在控制台会输出:hello 我们需要运行该文件 开始->运行 cmd 进入我们的程序目录 我的是D:/nodejs/hello.js 进入程序目录cd D:/nodej ...

  5. 九度OJ 1337:寻找最长合法括号序列 (DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:839 解决:179 题目描述: 给你一个长度为N的,由'('和')'组成的括号序列,你能找出这个序列中最长的合法括号子序列么?合法括号序列的 ...

  6. META-INF中的INF的意思

    1 META是元的意思,比如meta data,元数据. 2 什么是meta data 元数据就是描述其它数据的数据,比如web page中的meta data,包括关键字,对该网页的描述等等. 3 ...

  7. CentOS6下安装PHP7

    更新软件源[1] wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm wget http://rpm ...

  8. ThinkPHP5.0 用docker部署

    Dockerfile 文件如下: FROM hub.c.163.com/shenggen/thinkphp-docker:v0.0.1 ADD . /app RUN ["chmod" ...

  9. (转)RequireJS shim 用法说明

    RequireJS中如果使用AMD规范,在使用的过程中没有太多的问题,如果加载非AMD规范的JS文件,就需要使用Require中的shim. require.config({ paths:{ jque ...

  10. 数据库存储I/O类型分析与配置

    存储设备作为数据的容器,为应用提供数据存取服务,而存储系统将数据展现给不同的应用后,应用程序对数据访问不尽相同.简要来说,就是读和写,更加细分的话是以不同的传输单元(I/O大小)进行顺序和随机类型的读 ...