[LeetCode] 122. Best Time to Buy and Sell Stock II

夜歌乘年少發表於2024-07-02

medium是你的謊言.

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        #1
        if len(prices) == 1:
            return 0
        #else
        max_profit = 0
        min_price = prices[0]
        for i, element in enumerate(prices):
            #find a min_price 
            if element <= min_price:
                min_price = element
            else:
            #sell immediately
                max_profit += element - min_price
                min_price = element
        return max_profit

image

相關文章