美文网首页
LeetCode-121-买卖股票的最佳时机

LeetCode-121-买卖股票的最佳时机

作者: 阿凯被注册了 | 来源:发表于2020-09-29 22:15 被阅读0次
    image.png
    解题思路: 只允许交易一次,顺序遍历一次,寻找与最小买卖价格差异最大的price[i]。
    python3代码如下:
    class Solution:
        def maxProfit(self, prices: List[int]) -> int:
            minPrice = int(1e9)
            maxProfit =0
            for price in prices:
                if price < minPrice:
                    minPrice = price
                elif price-minPrice > maxProfit:
                    maxProfit = price-minPrice  
            return maxProfit
    

    相关文章

      网友评论

          本文标题:LeetCode-121-买卖股票的最佳时机

          本文链接:https://www.haomeiwen.com/subject/hlsyuktx.html