leetcode 121.买卖股票的最佳时机

从前往后遍历:

python 复制代码
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if (len(prices)==1):
            return 0

        max_profit = 0
        min_price = float('inf')
        for i in range(len(prices)):
            if prices[i] < min_price:
                min_price = prices[i]
            if max_profit < (prices[i]-min_price):
                max_profit = prices[i]-min_price
        
        return max_profit

        
相关推荐
野生技术架构师1 天前
金三银四面试总结篇,汇总 Java 面试突击班后的面试小册
java·面试·职场和发展
_深海凉_1 天前
LeetCode热题100-寻找两个正序数组的中位数
算法·leetcode·职场和发展
ja哇1 天前
大厂面试高频八股
java·面试·职场和发展
踩坑记录1 天前
leetcode hot100 寻找两个正序数组的中位数 hard 二分查找 双指针
leetcode
旖-旎1 天前
深搜练习(电话号码字母组合)(3)
c++·算法·力扣·深度优先遍历
谭欣辰1 天前
C++快速幂完整实战讲解
算法·决策树·机器学习
Mr_pyx1 天前
【LeetHOT100】随机链表的复制——Java多解法详解
算法·深度优先
AIFarmer1 天前
【无标题】
开发语言·c++·算法
AGV算法笔记1 天前
CVPR 2025 最新感知算法解读:GaussianLSS 如何用 Gaussian Splatting 重构 BEV 表示?
算法·重构·自动驾驶·3d视觉·感知算法·多视角视觉
勤劳的进取家1 天前
数据链路层基础
网络·学习·算法