leetcode-309-买卖股票的最佳时机含冷冻期

题目描述:

 

 方法一:动态规划 O(n) O(n)

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if len(prices) < 2:
            return 0
        n = len(prices)
        sell = [0] * n
        buy = [0] * n
        buy[0] = -prices[0]
        for i in range(1,n):
            sell[i] = max(sell[i-1],buy[i-1] + prices[i])
            buy[i] = max(buy[i-1],(sell[i - 2] if i > 1 else 0) - prices[i])
        return sell[-1]

 

posted @ 2020-07-11 21:06  oldby  阅读(156)  评论(0编辑  收藏  举报