123. Best Time to Buy and Sell Stock III

123. Best Time to Buy and Sell Stock III

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if len(prices)==0:return 0
        s1=s2=s3=s4=float('-inf')
        for i in range(len(prices)):
            s1=max(s1,-prices[i])
            s2=max(s2,s1+prices[i])
            s3=max(s3,s2-prices[i])
            s4=max(s4,s3+prices[i])
            # print(i,s1,s2,s3,s4)
        return max(0,s4)

状态转移

LeetCode - The World's Leading Online Programming Learning Platform

你可能感兴趣的:(leetcode)