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 ...
随机推荐
- @Retention(保留) 此注解用于运行时候(反射)时候使用 如果不使用的话 在反射时候无法获取到注解的值
@Retention(保留) 此注解用于运行时候(反射)时候使用 如果不使用的话 在反射时候无法获取到注解的值
- 【JavaScript&jQuery】返回顶部
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- TCP的拥塞控制 (一)
拥塞控制不同于流量控制,拥塞控制是在拥塞发生时,发送方根据一定的反馈,主动调节自己的发送速率,以防止拥塞恶化的行为. 1. 网络拥塞 路由器是网络中的关键组件,其内部有一定量的缓冲区,用于缓存来不 ...
- sql case用法举例
用一条sql语句查出学生表成绩小于60为不及格60-80为良好80-90为优秀 select name, case when 成绩<60 then 不及格 when 成绩>=60 and ...
- Linux内核分析第四周学习总结——系统调用的工作机制
Linux内核分析第四周学习总结--系统调用的工作机制 内核态 执行级别高,可以执行特权指令,访问任意物理地址,在intel X86 CPU的权限分级为0级. 用户态 执行级别低,只能访问0x0000 ...
- 解题:HNOI 2012 永无乡
题面 并查集维护连通性,然后暴力启发式合并就完了,记得合并时边DFS边清空数组 #include<cstdio> #include<cstring> #include<a ...
- BZOJ 3709&&AGC 018 C——多段排序的微扰法
BZOJ 3709• 有n只怪物,你的初始生命值为z.• 为了打败第i只怪物,你需要消耗cost[i]点生命值,但怪物死后会使你恢复val[i]点生命值.• 任何时候你的生命值都不能小于等于0.• 问 ...
- 【learning】中国剩余定理
问题描述 "今有物不知其数,三三数之余二,五五数之余三,七七数之余二.问物几何?" emmm这是..最开始这个问题被提出来的描述 其实说白了就是求解一次同余式组 然后还可以..解决 ...
- LeakCanary原理分析
参考文档 http://blog.csdn.net/wyfei021/article/details/46506521http://vjson.com/wordpress/leakcanary%e6% ...
- [LeetCode] 数学计算模拟类问题:加法,除法和幂,注意越界问题。题 剑指Offer,Pow(x, n) ,Divide Two Integers
引言 数学计算的模拟类题目,往往是要求实现某种计算(比如两数相除),实现的过程中会有所限定,比如不允许乘法等等. 这类题目首先要注意计算过程中本身的特殊情况.比如求相除,则必须首先反映过来除数不能为0 ...