Skip to content

Latest commit

 

History

History
executable file
·
137 lines (125 loc) · 4.42 KB

309. Best Time to Buy and Sell Stock with Cooldown.md

File metadata and controls

executable file
·
137 lines (125 loc) · 4.42 KB

309. Best Time to Buy and Sell Stock with Cooldown

Question

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 (ie, buy one and sell one share of the stock multiple times) with the following restrictions:

  • You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
  • After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
Example:

Input: [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]

Thinking:

  • Method 1: dp
    • 状态转移方程
      • buy[i - 1]:当前一天不买入,sell[i - 2] - prices[i - 1]:两天前卖出时的最大收益-当天股票价格。
      • buy[i] = Math.max(sell[i - 2] - prices[i - 1], buy[i - 1])
      • sell[i - 1]:前一天的卖出最大值, buy[i - 1] + prices[i - 1]:买入的最大值加上当天股票的价格。
      • sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i - 1])
class Solution {
    public int maxProfit(int[] prices) {
        if(prices == null || prices.length == 0) return 0;
        int len = prices.length;
        int[] buy = new int[len + 1];
        int[] sell = new int[len + 1];
        buy[1] = -prices[0];
        for(int i = 2; i <= len; i++){
            buy[i] = Math.max(sell[i - 2] - prices[i - 1], buy[i - 1]);
            sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i - 1]);
        }
        return sell[len];
    }
}

Second time

Imgur

class Solution {
    public int maxProfit(int[] prices) {
        if(prices == null || prices.length == 0) return 0;
        int len = prices.length;
        int[] s0 = new int[len + 1];
        int[] s1 = new int[len + 1];
        int[] s2 = new int[len + 1];
        s1[1] = -prices[0];
        for(int i = 2; i <= prices.length; i++){
            s0[i] = Math.max(s0[i - 1], s2[i - 1]);
            s1[i] = Math.max(s1[i - 1], s0[i - 1] - prices[i - 1]);
            s2[i] = s1[i - 1] + prices[i - 1];
        }
        return Math.max(s0[len], s2[len]);
    }
}

Third time

  • Method 1: dp
    • we have three states sold, hold and rest

    • Example: [1, 2, 3, 0, 2]

    • 0 price hold sold rest

    • 1 1 -1 -inf + 1 0

    • 2 2 -1 1 0

    • 3 3 -1 2 1

    • 4 0 1 -1 2

    • 5 2  1 3 2

    • Version 1:

       class Solution {
           public int maxProfit(int[] prices) {
               int len = prices.length;
               if(len <= 1) return 0;
               int[] hold = new int[len + 1];
               int[] sold = new int[len + 1];
               int[] rest = new int[len + 1];
               hold[1] = -prices[0];
               for(int i = 2; i <= len; i++){
                   hold[i] = Math.max(hold[i - 1], rest[i - 1] - prices[i - 1]);
                   sold[i] = hold[i - 1] + prices[i - 1];
                   rest[i] = Math.max(rest[i - 1], sold[i - 1]);
               }
               return Math.max(rest[len], sold[len]);
           }
       }
    • Version 2: state compress

       class Solution {
           public int maxProfit(int[] prices) {
               int len = prices.length;
               if(len <= 1) return 0;
               int hold = 0, sold = 0, rest = 0;
               int preHold = -prices[0];
               for(int i = 2; i <= len; i++){
                   hold = Math.max(preHold, rest - prices[i - 1]);            
                   rest = Math.max(rest, sold);
                   sold = preHold + prices[i - 1];
                   preHold = hold;
               }
               return Math.max(rest, sold);
           }
       }

Amazon Session

  • Method 1: dp
     class Solution {
     	public int maxProfit(int[] prices) {
     		if(prices == null || prices.length <= 1) return 0;
     		int len = prices.length;
     		int[] buys = new int[len + 1];
     		int[] sells = new int[len + 1];
     		int[] cools = new int[len + 1];
     		buys[1] = -prices[0];
     		for(int i = 2; i <= len; i++){
     			buys[i] = Math.max(buys[i - 1], cools[i - 1] - prices[i - 1]);
     			cools[i] = Math.max(cools[i - 1], sells[i - 1]);
     			sells[i] = Math.max(buys[i - 1] + prices[i - 1], sells[i - 1]);
     		}
     		return sells[len];
     	}
     }

Reference

  1. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown
  2. 花花酱 LeetCode 309. Best Time to Buy and Sell Stock with Cooldown - 刷题找工作 EP150