123.Best Time to Buy and Sell Stock III---dp
题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/
题目大意:与122题类似,只是这里要求买卖次数只能有两次,计算两次总共的最大利润值。
法一(超时):计算某一天之前的最大利润值与某一天之后的最大利润值,然后用for循环遍历得到最大值,时间复杂度是o(n^2),代码如下:
int res = 0;
for(int i = 0; i < prices.length; i++) {
int maxPre = 0;
int min = Integer.MAX_VALUE;
//0 to i-1
for(int j = 0; j < i; j++) {
if(prices[j] < min) {
min = prices[j];
}
else if(prices[j] - min > maxPre){
maxPre = prices[j] - min;
}
}
//i to n-1
int maxPost = 0;
min = Integer.MAX_VALUE;
for(int j = i; j < prices.length; j++) {
if(prices[j] < min) {
min = prices[j];
}
else if(prices[j] - min > maxPost){
maxPost = prices[j] - min;
}
} if(maxPre + maxPost > res) {
res = maxPre + maxPost;
}
}
return res;
法二(借鉴):动态规划,利用法一的思路,只是这里不用for循环嵌套遍历得到最大值,而是用两个数组分别记录某一天之前的最大利润值和某一天之后的最大利润值,然后再另开一个for循环,计算比较得到最大值。这里有两个动规公式:
1.数组记录最大利润:
1)某一天之前:preProfit[i] = max(preProfit[i - 1], prices[i] - min);
2)某一天之后:postProfit[i] = max(postProfit[i + 1], max - prices[i]);
2.for循环计算得到最大值:res = max(res, preProfit[i] + postProfit[i]);
代码如下(耗时1ms):
int length = prices.length;
int[] preProfit = new int[length];
int[] postProfit = new int[length];
//从前往后找,0 to i,用数组记录每一天i之前所能获得的最大利润,计算过程与121题类似
int minPrice = Integer.MAX_VALUE;
int max = 0;
for(int i = 0; i < length; i++) {
if(prices[i] < minPrice) {
minPrice = prices[i];
}
else if(prices[i] - minPrice > max) {
max = prices[i] - minPrice;
}
preProfit[i] = max;
}
//从后往前,i to n-1,用数组记录每一天i之后所能获得的最大利润
//注意:从后往前找的时候,应该记录当前位置之后的最大价值,然后将当前位置的价值与最大价值进行比较
int maxPrice = Integer.MIN_VALUE;
max = 0;
for(int i = length - 1; i >= 0; i--) {
if(prices[i] > maxPrice) {
maxPrice = prices[i];
}
else if(maxPrice - prices[i] > max) {
max = maxPrice - prices[i];
}
postProfit[i] = max;
} int res = 0;
for(int i = 0; i < length; i++) {
if(preProfit[i] + postProfit[i] > res) {
res = preProfit[i] + postProfit[i];
}
}
return res;
法三(借鉴):参考:http://blog.csdn.net/u012501459/article/details/46514309解法二。
123.Best Time to Buy and Sell Stock III---dp的更多相关文章
- LN : leetcode 123 Best Time to Buy and Sell Stock III
lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...
- LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【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]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 ...
- 【刷题-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 ...
- 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 a ...
- LeetCode OJ 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 ...
- 123. Best Time to Buy and Sell Stock III ——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 123. Best Time to Buy and Sell Stock III (Array; DP)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- AFNetworking你最最最起码要知道的基本使用~
AFNetworking是一个在iOS开发中,使用非常多的一个开源库 适用于iOS以及Mac OS X, 它构建于在(Apple iOS开发文档)NSURLConnection, NSOperatio ...
- bzoj2383[CEOI2011] ballons
题意 在一条数轴上从左向右有一些气球,每个气球一开始位于横坐标xi的位置,是半径为0的圆.现在开始从左向右给每个气球充气.被充气的气球的半径会不断变大,直到达到这个气球的半径上限Ri或者这个气球和之前 ...
- 【bzoj5146】有趣的概率 微积分
题目描述 "可爱的妹子就像有理数一样多,但是我们知道的,你在数轴上随便取一个点取到有理数的概率总是0,"芽衣在床上自顾自的说着这句充满哲理的话,"诶,柚子,我写完概率论的 ...
- 【bzoj1297】[SCOI2009]迷路 矩阵乘法
题目描述 给出一个 $n$ 个点的有向图,每条边的权值都在 $[1,9]$ 之间.给出 $t$ ,求从 $1$ 到 $n$ ,经过路径边权和恰好为 $t$ 的方案数模2009. 输入 第一行包含两个整 ...
- C++解析(17):操作符重载
0.目录 1.操作符重载 2.完善的复数类 3.小结 1.操作符重载 下面的复数解决方案是否可行? 示例1--原有的解决方案: #include <stdio.h> class Compl ...
- Ajax+Js局部刷新
通过 AJAX,JavaScript 可使用 JavaScript 的 XMLHttpRequest 对象来直接与服务器进行通信.通过这个对象, JavaScript 可在不重载页面的情况与 Web ...
- MSSQL DBA权限获取WEBSHELL的过程
前言 本文主要通过一个案例来演示一下当MSSQL是DBA权限,且不知道路径的时候如何去获取WEBSHELL.当然这种方式对站库分离的无效.我测试的环境是在Win7 64位下,数据库是SQLServer ...
- 专题训练之区间DP
例题:以下例题部分的内容来自https://blog.csdn.net/my_sunshine26/article/details/77141398 一.石子合并问题 1.(NYOJ737)http: ...
- 【生成树,堆】【CF1095F】 Make It Connected
Description 给定 \(n\) 个点,每个点有点权,连结两个点花费的代价为两点的点权和.另外有 \(m\) 条特殊边,参数为 \(x,y,z\).意为如果你选择这条边,就可以花费 \(z\) ...
- 《剑指offer》— JavaScript(4)重建二叉树
重建二叉树 题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序 ...