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

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

posted @ 2024-07-02 00:41  夜歌乘年少  阅读(2)  评论(0编辑  收藏  举报