【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;

    }
}
相关推荐
闲研随记22 分钟前
RL算法学习:ArgMaxRL
算法·llm·强化学习·rl
飞Link25 分钟前
零基础:离散数据积分与 Python 实现保姆级教程
python·算法
圣保罗的大教堂27 分钟前
leetcode 836. 矩形重叠 简单
leetcode
淡海水40 分钟前
11-03-Unity-List-T-和Dictionary-TKey-TValue-的性能调优实战
算法·unity·c#·list·dictionary
土司大王44 分钟前
LeetCode hot100——394.字符串解码:Java 双栈模拟
java·算法·leetcode
a187927218311 小时前
【算法】双指针与滑动窗口(三):相向双指针——比较、排除、收缩
算法·leetcode·双指针·滑动窗口·原理·相向双指针·算法讲解
AI 小老六1 小时前
Agent 记忆系统难在取舍
人工智能·算法·架构·agent·memory·harness
大熊背1 小时前
《Color constancy by characterization of illumination chromaticity》之色度色域最大化算法(一)
数码相机·算法·白平衡·色度色域
不会就选b1 小时前
算法日常・每日刷题--<贪心>13
算法
带多刺的玫瑰1 小时前
Leecode#35刷题之搜索插入位置
java·python·算法