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. IOS启动页动画(uiview 淡入淡出效果 )2

    Appdelegate里面右个这个函数,只要它没结束,你的等待界面就不会消失.以在启动的时候做些动画 - (BOOL)application:(UIApplication *)application ...

  2. C语言中的指针运算

    int a[5]={1,2,3 ,4,5} *p=a; *p++ 等价于*(p++) 等价于a[i++](i++ i首先会被使用任何进行自+) *++p等价于*(++p) 等价于 a[++i] (++ ...

  3. Apache禁止ip访问

    网站突然让禁止ip访问,于是就通过配置Apache达到了想要的效果. 我们网站用的是Apache+tomcat集群,所以需要配置虚拟主机,虚拟主机我在这里就不说了,不明白的上网搜搜吧,这里只说禁止ip ...

  4. Netty实现java多线程Post请求解析(Map参数类型)—SKY

    netty解析Post的键值对 解析时必须加上一个方法,ch.pipeline().addLast(new HttpObjectAggregator(2048)); 放在自己的Handel前面. ht ...

  5. 【python】-- 协程介绍及基本示例、协程遇到IO操作自动切换、协程(gevent)并发爬网页

    协程介绍及基本示例 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是协程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他 ...

  6. Redis通过PUBLISH / SUBSCRIBE 等命令实现了订阅与发布模式

    # 切换目录 [root@localhost /]# cd /opt/redis-4.0.10 # 启动客户端 -p 指定端口 [root@localhost ~]# redis-cli -p 638 ...

  7. 使用微软官方U盘制作软件来安装纯净版windows

    第一步:下载一个制作U启的工具;windows-usb-dvd-download-tool 微软官网:https://www.microsoft.com/en-us/download/windows- ...

  8. 关于VMAX中存储资源池(SRP)

    Storage Resource Pool中的相关元素 SRP由一个或多个数据池组成,这些数据池包含了预配置的数据(或TDAT)设备,可为创建和呈现给主机与应用程序的精简设备(TDEVS) 提供存储. ...

  9. 第14条:尽量用异常来表示特殊情况,而不要返回Nono

    核心知识点: 1.用None这个返回值来表示特殊意义的函数,很容易使调用者犯错,因为None和0以及空字符串之类的值,在条件表达式里都会评估为False. 2.两种方法:二元法:将异常抛给上一级直接报 ...

  10. parent

    <?php class MyObject { function myMethod() { //标准功能 echo "Standard Functionality\n"; } ...