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

想清楚了确实算是简单题.

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

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