1. 程式人生 > >122. Best Time to Buy and Sell Stock II (Array)

122. Best Time to Buy and Sell Stock II (Array)

##### 題目描述:

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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Example 1:

Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
             Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.

Example 2:

Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
             Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
             engaging multiple transactions at the same time. You must sell before buying again.

##### 分析:

和另外一道類似,但是不同的在於可以多次買賣,但是手中同時進行的交易不能大於1個。

###### 方法一:

用max儲存最大值,min儲存最小值,res儲存臨時收益,profit儲存最終收益,噹噹前價格比max小時應該結束當前交易,從當前價格買入,然後更新min,max都是為當前價格,temp加入profit,temp歸零;如果當前價格比max大時,繼續當前交易,更新max。時間複雜度O(n),空間複雜度O(1)

 public int maxProfit(int[] prices) {
        if(prices==null ||prices.length==0)
            return 0;
        int min=prices[0], max=prices[0];
        int profit=0;
        int temp = max-min;
        for(int i=1;i<prices.length;i++){               
            if(prices[i]<max){
                temp = max-min;
                profit += temp;
                temp = 0;
                min = prices[i];
                max = prices[i];
            }
            else{
                max = prices[i];
                temp = max-min;
                max = prices[i];
            }
        }
        profit += temp;
        return profit;
    }

###### 方法二:Peak Valley Approach

TotalProfit=∑i​(height(peaki​)−height(valleyi​))

Profit Graph

和方法一差不多,在一個while迴圈中首先找valley,遇到升高就開始找peak.再次降低結束這次迴圈。繼續開始搜尋下一個上坡。

  • Time complexity : O(n)O(n). Single pass.

  • Space complexity : O(1)O(1). Constant space required. 

public int maxProfit(int[] prices) {
        int i = 0;
        int valley = prices[0];
        int peak = prices[0];
        int maxprofit = 0;
        while (i < prices.length - 1) {
            while (i < prices.length - 1 && prices[i] >= prices[i + 1])
                i++;
            valley = prices[i];
            while (i < prices.length - 1 && prices[i] <= prices[i + 1])
                i++;
            peak = prices[i];
            maxprofit += peak - valley;
        }
        return maxprofit;
    }

方法三:

只關注上坡,然後將每一段的profit相加

Profit Graph

  • Time complexity : O(n)O(n). Single pass.

  • Space complexity : O(1)O(1). Constant space required.

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