【leetcode刷题笔记】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).
题解: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的更多相关文章
- 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 ...
- 【刷题-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 ...
- 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 ...
- 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 ...
- 【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(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 ...
- 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 ...
- 【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 ...
- [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 ...
随机推荐
- 怎样去掉a标签的蓝框
直接上代码 *{-webkit-tap-highlight-color:rgba(255,0,0,0);} 我是直接给每一个都加了这个属性 其实没有必要 因为 只有a 标签 input 标签 和t ...
- Java对文件夹中的文件按修改时间排序
import java.io.File; import java.util.Arrays; import java.util.Comparator; import java.util.Date; pu ...
- (转)linux设备驱动之USB数据传输分析 一
三:传输过程的实现说到传输过程,我们必须要从URB开始说起,这个结构的就好比是网络子系统中的skb,好比是I/O中的bio.USB系统的信息传输就是打成URB结构,然后再过行传送的.URB的全称叫US ...
- Amr and Chemistry
C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
- PAT 1061. 判断题(15)
判断题的评判很简单,本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分. 输入格式: 输入在第一行给出两个不超过100的正整数N和M,分别是学生人数和判断题数量.第二行给出M个不超过5的正 ...
- vs+opencv
vs + opencv 配置见网页 https://blog.csdn.net/qq_17550379/article/details/78201442 统一所有工程配置见下图:
- Navicat Premium试用期破解方法(转)
转载网址https://blog.csdn.net/Jason_Julie/article/details/82864187 1.按步骤安装Navicat Premium,如果没有可以去官网下载:ht ...
- 牛客小白月赛1 E 圆与三角形 【数学】
题目链接 https://www.nowcoder.com/acm/contest/85/E 思路 在三角形中,这一串东西的值恒为1 又 SIN A 的最大值 为1 所以 这串式子的最大值 就是 r ...
- iOS 开发规范
公司来了大牛 是绝好的学习机会 今天分享了我们一个代码规范 比如UITableViewCell 1.首先根据这个cell 需要的数据源 建一个数据model ,只针对于 该cell 好处:数据独立 ...
- ubuntu14.04 在自带python2.7上安装python3.3.5 可以用但是有问题
一开始写的时候并没有发现这么安装有问题,后来发现问题也不想删了,当个教训,如果想安装从python自带版本换别的版本的话就别接着看了,这么安装有问题.需要进行配置,但是我还不会.其实下面只是差了一步配 ...