分享

Best Time to Buy and Sell Stock系列

 雪柳花明 2016-10-04

Best Time to Buy and Sell Stock I

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.

题目链接:https://oj./problems/best-time-to-buy-and-sell-stock/

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

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

public class Solution {

  public int maxProfit(int[] prices) {
    if(prices.length == 0) return 0;
    int low = prices[0];
    int ans = 0;
    for(int i=1; i<prices.length; i++){
      if(prices[i] < low) low = prices[i];
      else if(prices[i] - low > ans) ans = prices[i] - low;
    }
    return ans;
  }
}

 Best Time to Buy and Sell Stock II

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

题目链接:https://oj./problems/best-time-to-buy-and-sell-stock-ii/

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

public class Solution {
    public int maxProfit(int[] prices) {
      if(prices.length == 0) return 0;
      int ans = 0;
      for(int i=1; i<prices.length; i++){
          if(prices[i] > prices[i-1])
            ans += prices[i]-prices[i-1];
      }
      return ans;
    }
}

 Best Time to Buy and Sell Stock III

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.

题目链接:https://oj./problems/best-time-to-buy-and-sell-stock/

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

代码如下:

public class Solution {
  public int maxProfit(int[] prices) {
    int ans = 0;
    for(int m = 0; m<prices.length; m++){
      int tmp = maxProfitOnce(prices, 0, m) + maxProfitOnce(prices, m, prices.length-1);
      if(tmp > ans) ans = tmp;
    }
    return ans;
  }

  public int maxProfitOnce(int[] prices,int start, int end){
    if(start >= end) return 0;
    int low = prices[start];
    int ans = 0;
    for(int i=start+1; i<=end; i++){
      if(prices[i] < low) low = prices[start];
      else if(prices[i] - low > ans) ans = prices[i] - low;
    }
    return ans;
  }

}

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

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

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

public class Solution {
    public int maxProfit(int[] prices) {
  if(prices.length == 0) return 0;
  int ans = 0;
  int n = prices.length;

  //正向遍历,opt[i]表示 prices[0...i]内做一次交易的最大收益.
  int opt[] = new int[n];
  opt[0] = 0;
  int low = prices[0];
  int curAns = 0;
  for(int i = 1; i<n; i++){
      if(prices[i] < low) low = prices[i];
      else if(curAns < prices[i] - low) curAns = prices[i] - low;
      opt[i] = curAns;
  }

  //逆向遍历, opt[i]表示 prices[i...n-1]内做一次交易的最大收益.
  int optReverse[] = new int[n];
  optReverse[n - 1] = 0;
  curAns = 0;
  int high = prices[n - 1];
  for(int i=n-2; i>=0; i--){
      if(prices[i] > high) high = prices[i];
      else if(curAns < high - prices[i]) curAns = high - prices[i];
      optReverse[i] = curAns;
  }

  //再进行划分,分别计算两个部分
  for(int i=0; i<n; i++){
      int tmp = opt[i] + optReverse[i];
      if(ans < tmp) ans = tmp;
  }
  return ans;
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多