LeetCodeDiary

A Diary for solving LeetCode problems

View on GitHub
'''
Description: 
Autor: Au3C2
Date: 2021-03-29 18:53:23
LastEditors: Au3C2
LastEditTime: 2021-03-29 18:53:23
'''
class Solution:
    def maxProfit(self, prices: List[int]) -> int:

        if not prices:
            return 0
        
        n = len(prices)
        f0, f1, f2 = -prices[0], 0, 0
        for i in range(1, n):
            newf0 = max(f0, f2 - prices[i])
            newf1 = f0 + prices[i]
            newf2 = max(f1, f2)
            f0, f1, f2 = newf0, newf1, newf2
        
        return max(f1, f2)

动态规划,中等 股票买卖冷冻期版 https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/