leetcode-python-买卖股票的最佳时机

从头遍历到尾两两计算,遇到最小换最小,逐个计算大利润,遇到大利润保存大利润

类似栈

效率高但是耗内存

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if len(prices) <= 1:
            return 0
        minimum = prices[0]
        profit = 0
        for i in range(1,len(prices)):
            minimum = minimum if minimum < prices[i] else prices[i]
            profit = profit if profit > prices[i] - minimum else prices[i] - minimum
        return profit

 

posted @ 2021-06-07 15:19  泊鸽  阅读(104)  评论(0)    收藏  举报