[LeetCode] 121. Best Time to Buy and Sell Stock

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

想清楚了確實算是簡單題.

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):
            if element <= min_price:
                min_price = element
            elif element - min_price > max_profit:
                max_profit = element - min_price
        return max_profit

image

相關文章