【LeetCode热题100】--121.买卖股票的最佳时机

121.买卖股票的最佳时机

java 复制代码
class Solution {
    public int maxProfit(int[] prices) {
        int minprice = Integer.MAX_VALUE;
        int maxprofit = 0;
        for(int i =0;i<prices.length;i++){
            if(prices[i] < minprice){
                minprice = prices[i]; //找到最小值
            }else if(prices[i] - minprice > maxprofit){ //记录之后与最小值之间得差值
                maxprofit = prices[i] - minprice;  
            }
        }
        return maxprofit;

    }
}
相关推荐
学逆向的10 分钟前
汇编——位运算
开发语言·汇编·算法·网络安全
可编程芯片开发24 分钟前
基于simulink的PEM燃料电池控制系统建模与仿真,对比PID,积分分离以及滑模控制器
算法
学究天人40 分钟前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷2)
算法·数学建模·动态规划·图论·抽象代数·拓扑学
玛卡巴卡ldf1 小时前
【LeetCode 手撕算法】(细节知识点总结)
java·数据结构·算法·leetcode·力扣
wanzehongsheng2 小时前
零碳产业园光伏园区光伏电站追踪对比固定:发电增益技术边界分析
算法·光伏发电·光伏·零碳园区·太阳能追光·低碳环保·追踪电站
KobeSacre2 小时前
CyclicBarrier 源码
java·jvm·算法
手写码匠2 小时前
注意力机制全家桶:从 Multi-Head 到 GQA 再到 Flash Attention 的手写实现
人工智能·深度学习·算法·aigc
大鱼>2 小时前
模型公平性与偏差检测:AI伦理实战指南
人工智能·深度学习·算法·机器学习
学究天人3 小时前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷3.2)
线性代数·算法·机器学习·数学建模·动态规划·抽象代数·拓扑学
tkevinjd3 小时前
力扣322-零钱兑换
算法·leetcode·动态规划