309. Best Time to Buy and Sell Stock with Cooldown

# Medium

很难想,直接看代码吧。

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        buy = float('-inf')
        pre_buy, pre_sell, sell = 0, 0, 0
        for price in prices:
            pre_buy = buy
            buy = max(pre_sell-price, pre_buy)
            pre_sell = sell
            sell = max(pre_buy+price, pre_sell)

            
        return sell

Last updated

Was this helpful?