力扣每日一练(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;
}

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

相关推荐
图灵学术计算机论文辅导25 分钟前
论文推荐|迁移学习+多模态特征融合
论文阅读·人工智能·深度学习·计算机网络·算法·计算机视觉·目标跟踪
threejs源码翻译官1 小时前
显微镜图像处理【优化】- 使用图像风格迁移技术放大图像细节
算法
强德亨上校1 小时前
贪心算法(Greedy Algorithm)详解
算法·贪心算法
浮灯Foden2 小时前
算法-每日一题(DAY13)两数之和
开发语言·数据结构·c++·算法·leetcode·面试·散列表
西工程小巴3 小时前
实践笔记-VSCode与IDE同步问题解决指南;程序总是进入中断服务程序。
c语言·算法·嵌入式
Tina学编程3 小时前
48Days-Day19 | ISBN号,kotori和迷宫,矩阵最长递增路径
java·算法
Moonbit3 小时前
MoonBit Perals Vol.06: MoonBit 与 LLVM 共舞 (上):编译前端实现
后端·算法·编程语言
执子手 吹散苍茫茫烟波4 小时前
leetcode415. 字符串相加
java·leetcode·字符串
百度Geek说5 小时前
第一!百度智能云领跑视觉大模型赛道
算法
big_eleven5 小时前
轻松掌握数据结构:二叉树
后端·算法·面试