LeetCode 热题100-45-买卖股票的最佳时机

LeetCode 热题100-45-买卖股票的最佳时机,第1张

核心思路:动态规划
思路:
DP式:第i天的最大收益=MAX{前i-1天的最大收益,第i天价格-前i-1天的最低价格}

class Solution {
    public int maxProfit(int[] prices) {
        if(prices.length < 2){
            return 0;
        }
        int max = 0;
        int min = prices[0];
        for(int i = 1; i < prices.length; i++){
            max = Math.max(max,prices[i]-min);
            min = Math.min(min,prices[i]);
        }
        return max;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/585130.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-12
下一篇2022-04-12

发表评论

登录后才能评论

评论列表(0条)

    保存