0
点赞
收藏
分享

微信扫一扫

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

未定义变量 2022-04-13 阅读 48
leetcodejava

核心思路:动态规划
思路:
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;
    }
}
举报

相关推荐

0 条评论