力扣每日一练(24-1-18)

经验一:不要把问题想复杂

Python:

python 复制代码
min_price = float('inf')
        max_profit = 0
        for price in prices:
            min_price = min(min_price, price)
            max_profit = max(max_profit, price - min_price)
        return max_profit

C#:

cs 复制代码
public int MaxProfit(int[] prices) {
    int minPrice = Int32.MaxValue;
    int maxProfit = 0;
    foreach (int price in prices) {
        minPrice = Math.Min(minPrice, price);
        maxProfit = Math.Max(maxProfit, price - minPrice);
    }
    return maxProfit;
}

一开始我想到的是引入波峰波谷的概念,但是不适用,想复杂了。

相关推荐
报错小能手1 小时前
C++笔记(面向对象)静态联编和动态联编
开发语言·c++·算法
WBluuue1 小时前
AtCoder Beginner Contest 430(ABCDEF)
c++·算法
小肖爱笑不爱笑1 小时前
2025/11/5 IO流(字节流、字符流、字节缓冲流、字符缓冲流) 计算机存储规则(ASCII、GBK、Unicode)
java·开发语言·算法
熬了夜的程序员2 小时前
【LeetCode】99. 恢复二叉搜索树
算法·leetcode·职场和发展
Kent_J_Truman2 小时前
LeetCode Hot100 自用
算法·leetcode·职场和发展
还是码字踏实2 小时前
算法题种类与解题思路全面指南:基于LeetCode Hot 100与牛客Top 101
算法·leetcode
Victory_orsh3 小时前
“自然搞懂”深度学习(基于Pytorch架构)——010203
人工智能·pytorch·python·深度学习·神经网络·算法·机器学习
CoovallyAIHub3 小时前
突破360°跟踪极限!OmniTrack++:全景MOT新范式,HOTA指标狂飙43%
深度学习·算法·计算机视觉
得物技术3 小时前
得物管理类目配置线上化:从业务痛点到技术实现
后端·算法·数据分析
CoovallyAIHub4 小时前
首个大规模、跨模态医学影像编辑数据集,Med-Banana-50K数据集专为医学AI打造(附数据集地址)
深度学习·算法·计算机视觉