0309. Best Time to Buy and Sell Stock with Cooldown
Medium | Array + DP | 32 ms (96.27%), 14.6 MB (38.63%)
Input: prices = [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]
Input: prices = [1]
Output: 0Last updated
Medium | Array + DP | 32 ms (96.27%), 14.6 MB (38.63%)
Input: prices = [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]
Input: prices = [1]
Output: 0Last updated
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# (base case)
if len(prices) == 0 or len(prices) == 1: return 0
# ==================================================
# Array + Dynamic Programming (FSM) =
# ==================================================
# time : O(n)
# space : O(1)
hold, noHold = float('-inf'), 0
# after selling stock, you cannot buy stock on the next day = NO HOLD state
preDaySell = 0
for price in prices:
preHold, preNoHold = hold, noHold
# due to cooldown, cannot use preNoHold's profit to buy stock, use preDaySell
hold = max(preHold, preDaySell - price)
noHold = max(preNoHold, preHold + price)
# record at the end to have an one-day gap (still 0 after 1st iteration)
preDaySell = preNoHold
return noHoldclass Solution {
/**
* @time : O(n)
* @space : O(1)
*/
public int maxProfit(int[] prices) {
/* base case */
if(prices.length == 1) return 0;
int hold = Integer.MIN_VALUE, noHold = 0;
int preDaySell = 0;
for(int price: prices) {
int preHold = hold, preNoHold = noHold;
hold = Math.max(preHold, preDaySell - price);
noHold = Math.max(preNoHold, preHold + price );
preDaySell = preNoHold;
}
return noHold;
}
}