best-time-to-buy-and-sell-stock系列——先买入后卖出股票的最大值

1、

Say you have an array for which the i th element is the price of a given stock on day  i .

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

给一个数prices[],prices[i]代表股票在第i天的售价,求出只做一次交易(一次买入和卖出)能得到的最大收益。

只需要找出最大的差值即可,即 max(prices[j] – prices[i]) ,i < j。一次遍历即可,在遍历的时间用遍历low记录 prices[o....i] 中的最小值,就是当前为止的最低售价,时间复杂度为 O(n)。

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int> &prices) {
 4         if(prices.empty())
 5             return 0;
 6         int res=0,min=prices[0];
 7         for(int i=0;i<prices.size();i++){
 8             if(prices[i]<min) min=prices[i];
 9                 else if(prices[i]-min>res) res=prices[i]-min;
10         }
11         return res;
12     }
13 };

2、

Say you have an array for which the i th element is the price of a given stock on day  i .

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

此题和上面一题的不同之处在于不限制交易次数。也是一次遍历即可,只要可以赚就做交易。

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int> &prices) {
 4         if(prices.empty()) return 0;
 5         int res =0;
 6         for(int i=1;i<prices.size();i++){
 7             if(prices[i]-prices[i-1]>0)
 8                 res+=prices[i]-prices[i-1];
 9         }
10         return res;
11     }
12 };

3、

Say you have an array for which the i th element is the price of a given stock on day  i .

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

此题是限制在两次交易内,相对要难一些。容易想到的解决办法是,把prices[] 分成两部分prices[0...m] 和 prices[m...length]  ,分别计算在这两部分内做交易的做大收益。由于要做n次划分,每次划分可以采用 第一题:  I的解法, 总的时间复杂度为O(n^2).

 1 public class Solution {
 2     public int maxProfit(int[] prices) {
 3         int ans = 0;
 4         for(int m = 0; m<prices.length; m++){
 5             int tmp = maxProfitOnce(prices, 0, m) + maxProfitOnce(prices, m, prices.length-1);
 6             if(tmp > ans) ans = tmp;
 7         }
 8         return ans;
 9     }
10 
11     public int maxProfitOnce(int[] prices,int start, int end){
12         if(start >= end) return 0;
13         int low = prices[start];
14         int ans = 0;
15         for(int i=start+1; i<=end; i++){
16             if(prices[i] < low) low = prices[start];
17             else if(prices[i] - low > ans) ans = prices[i] - low;
18         }
19         return ans;
20     }
21 
22 }

但是由于效率过低,运行超时。可以利用动态规划的思想进行改进,保持计算的中间结果,减少重复的计算。

那就是第一步扫描,先计算出子序列[0,...,i]中的最大利润,用一个数组保存下来,那么时间是O(n)。计算方法也是利用第一个问题的计算方法。 第二步是逆向扫描,计算子序列[i,...,n-1]上的最大利润,这一步同时就能结合上一步的结果计算最终的最大利润了,这一步也是O(n)。 所以最后算法的复杂度就是O(n)的。

就是说,通过预处理,把上面的maxProfitOnce()函数的复杂度降到O(1)

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int> &prices) {
 4         if(prices.empty()) return 0;
 5         int n=prices.size();
 6         vector<int> opt(n,0);
 7         int res=0,low=prices[0];
 8         for(int i=0;i<n;i++){
 9             if(prices[i]<low) low=prices[i];
10             else if(res <prices[i]-low) res=prices[i]-low;
11             opt[i]=res;
12         }
13         vector<int> optReverse(n,0);
14         int high=prices[n-1];
15         res=0;
16         for(int i=n-2;i>=0;i--){
17             if(prices[i]>high) high=prices[i];
18             else if(high-prices[i]>res) res=high-prices[i];
19             optReverse[i]=res;
20         }
21         res=0;
22         
23         for(int i=0;i<n;i++){
24             int tmp=opt[i]+optReverse[i];
25             res=tmp>res?tmp:res;
26         }
27         return res;
28     }
29 };
原文地址:https://www.cnblogs.com/zl1991/p/7001628.html