【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 ...
随机推荐
- 《转》PyQt4 精彩实例分析* 实例2 标准对话框的使用
和大多数操作系统一样,Windows及Linux都提供了一系列的标准对话框,如文件选择,字体选择,颜色选择等,这些标准对话框为应用程序提供了一致的观感.Qt对这些标准对话框都定义了相关的类.这些类让使 ...
- MySQL复制经常使用拓扑结构具体解释
复制的体系结构有下面一些基本原则: (1) 每一个slave仅仅能有一个master: (2) 每一个slave仅仅能有一个唯一的serverID: (3) 每一个master能够有 ...
- thinkphp自动验证无效的问题
新手入门thinkphp,试用自动验证表单输入数据功能,却发现怎么都不能调用自动验证,自动验证无效,原因竟是一个小细节的疏忽,学习一定要细心啊! Action方法: IndexAction下的adds ...
- 爬虫入门【1】urllib.request库用法简介
urlopen方法 打开指定的URL urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, ca ...
- 让Xcode支持高版本系统设备真机测试
最新支持11.2 (15C107) Xcode只可以支持iPhone手机对应iOS系统以下的真机测试.一般想要支持最新的iPhone手机系统,有两个方法. 第一.就需要更新Xcode,这一个方法有一个 ...
- ElasticSearch(二十)定位不合法的搜索及其原因
GET /test_index/test_type/_validate/query?explain { "query": { "math": { "t ...
- thymeleaf基本应用
Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
- Hadoop初体验
1.首先准备环境 系统:Linux(centOS) jdk:1.7 这里jdk要安装配置完成,具体步骤参考:Linux环境下安装JDK 注意:本次没有配置免密登录,所以在启动和停止的时候回让你输入多次 ...
- easyui的 一些经验
1. 渲染网络表格时,行操作 <th field="sort_num" width="10" data-options="field:'id', ...
- Android Studio实现代码混淆
1,在build.grandle添加,其中规则写在proguard-rules.pro中,也可以自定义一个文件,将其代替,比如eclipse常用的 proguard-project.txt: bui ...