LeetCode: Best Time to Buy and Sell Stock III 解题报告

 


Best Time to Buy and Sell Stock III
Question Solution
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).

解答:
1. 从左往右扫描,计算0-i的这个区间的最大利润。方法可以参见股票第一题
2. 从右往左扫描,计算i-len这个区间的最大利润。方法同上。
3. 再从头至尾扫一次,每个节点加上左边和右边的利润。记录最大值。

复制一点别人的讲解:

O(n^2)的算法很容易想到:

找寻一个点j,将原来的price[0..n-1]分割为price[0..j]和price[j..n-1],分别求两段的最大profit。

进行优化:

对于点j+1,求price[0..j+1]的最大profit时,很多工作是重复的,在求price[0..j]的最大profit中已经做过了。

类似于Best Time to Buy and Sell Stock,可以在O(1)的时间从price[0..j]推出price[0..j+1]的最大profit。

但是如何从price[j..n-1]推出price[j+1..n-1]?反过来思考,我们可以用O(1)的时间由price[j+1..n-1]推出price[j..n-1]。

最终算法:

数组l[i]记录了price[0..i]的最大profit,

数组r[i]记录了price[i..n]的最大profit。

已知l[i],求l[i+1]是简单的,同样已知r[i],求r[i-1]也很容易。

最后,我们再用O(n)的时间找出最大的l[i]+r[i],即为题目所求。(最后一步可以合并在第二步中)。

REF: http://blog.csdn.net/pickless/article/details/12034365

代码1:

 1 public int maxProfit(int[] prices) {
 2         if (prices == null || prices.length == 0) {
 3             return 0;
 4         }
 5         
 6         int len = prices.length;
 7         int[] left = new int[len];
 8         int[] right = new int[len];
 9         
10         int min = prices[0];
11         left[0] = 0;
12         for (int i = 1; i < len; i++) {
13             min = Math.min(min, prices[i]);
14             left[i] = Math.max(left[i - 1], prices[i] - min);
15         }
16         
17         int max = prices[len - 1];
18         right[len - 1] = 0;
19         for (int i = len - 2; i >= 0; i--) {
20             max = Math.max(max, prices[i]);
21             right[i] = Math.max(right[i + 1], max - prices[i]);
22         }
23         
24         int rst = 0;
25         for (int i = 0; i < len; i++) {
26             rst = Math.max(rst, left[i] + right[i]);
27         }
28         
29         return rst;
30     }
View Code

代码2:

 1 public class Solution {
 2     public int maxProfit(int[] prices) {
 3         if (prices == null) {
 4             return 0;
 5         }
 6         
 7         int ret = 0;
 8         
 9         int len = prices.length;
10         int[] leftProfile = new int[len];
11         int profile = 0;
12         
13         int min = Integer.MAX_VALUE;
14         for (int i = 0; i < len; i++) {
15             min = Math.min(min, prices[i]);
16             profile = Math.max(profile, prices[i] - min);
17             leftProfile[i] = profile;
18         }
19         
20         int max = Integer.MIN_VALUE;
21         profile = 0;
22         for (int i = len - 1; i >= 0; i--) {
23             max = Math.max(max, prices[i]);
24             profile = Math.max(profile, max - prices[i]);
25             
26             // sum the left profit and the right profit.
27             ret = Math.max(ret, profile + leftProfile[i]);
28         }
29         
30         return ret;
31     }
32 }
View Code

 DP思路:

 1 // DP solution:
 2     public int maxProfit(int[] prices) {
 3         if (prices == null || prices.length == 0) {
 4             return 0;
 5         }
 6         
 7         int ret = 0;
 8         
 9         int len = prices.length;
10         int[] leftProfile = new int[len];
11         
12         int min = prices[0];
13         leftProfile[0] = 0;
14         for (int i = 1; i < len; i++) {
15             min = Math.min(min, prices[i]);
16             leftProfile[i] = Math.max(leftProfile[i - 1], prices[i] - min);
17         }
18         
19         int max = Integer.MIN_VALUE;
20         int profile = 0;
21         for (int i = len - 1; i >= 0; i--) {
22             max = Math.max(max, prices[i]);
23             profile = Math.max(profile, max - prices[i]);
24             
25             // sum the left profit and the right profit.
26             ret = Math.max(ret, profile + leftProfile[i]);
27         }
28         
29         return ret;
30     }
View Code

 

GitHub代码链接

posted on 2015-01-03 18:51  Yu's Garden  阅读(791)  评论(0编辑  收藏  举报

导航