题目链接: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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 【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 ...

  4. [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 ...

  5. 【刷题-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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

  10. 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 ...

随机推荐

  1. Exception异常 自定义异常

    public class Exception extends Throwable Exception 类及其子类是 Throwable 的一种形式,它指出了合理的应用程序想要捕获的条件. public ...

  2. Codeforces 576D. Flights for Regular Customers(倍增floyd+bitset)

    这破题调了我一天...错了一大堆细节T T 首先显然可以将边权先排序,然后逐个加进图中. 加进图后,倍增跑跑看能不能到达n,不能的话加新的边继续跑. 倍增的时候要预处理出h[i]表示转移矩阵的2^0~ ...

  3. [学习笔记]搜索——模拟与dp的结合

    搜索: 一种基础的算法. 考察常见于NOIP 但是高级的搜索算法可能还会在省选出现. 50%以上的暴力都可以用搜索直接枚举来写. 但是,当数据规模不是很大的时候,搜索也可能成为正解. (比如剪枝PK状 ...

  4. go日期时间函数+常用内建函数+错误处理

    日期时间函数 // 时间日期函数包 import "time" // 1. 当前时间 time.Now()-->time.Time类型 // 2. now:=time.Now ...

  5. (二)SMO算法

    11 SMO优化算法(Sequential minimal optimization) SMO算法由Microsoft Research的John C. Platt在1998年提出,并成为最快的二次规 ...

  6. PID控制算法的C语言实现十 专家PID与模糊PID的C语言实现

    本节是PID控制算法的C语言实现系列的最后一节,前面8节中,已经分别从PID的实现到深入的过程进行了一个简要的讲解,从前面的讲解中不难看出,PID的控制思想非常简单,其主要问题点和难点在于比例.积分. ...

  7. 如何将下载的web工程导入到eclipse中使用

    如果你是喜欢编程的,在你的开发工具中一定有许多项目,就像小编一样(PS:小编只想默默地装一X):   我们选中其中的一个项目,然后[Ctrl + C]复制,再[Ctrl + V]粘贴到桌面:   那么 ...

  8. 010. C++ 传值与传引用

    1.参数传递 参数传递:pass by value vs. pass by reference(to const) 推荐:能传引用,尽量传引用(高效,尤其在需要拷贝的对象很大时) class comp ...

  9. codeforces 55D 数位dp

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  10. mobiscroll 案例git

    https://github.com/zhoushengmufc/iosselect