Leetcode 309 Best Time to Buy and Sell Stock with Cooldown


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)
莫名想到了狀態機。
整個過程在sell 和 buy之間轉換,他們之間的關係如下:
最後一定是sell結尾,所以返回的是s0


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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章