LeeCode121买卖股票的最佳时机(Java)(双指针)

题目链接:LeeCode121买卖股票的最佳时机
题目描述:在这里插入图片描述
当前天最大盈利是今天价格减之前天里最小的价格,依次找

class Solution {
    
    
    public static int maxProfit(int[] prices) {
    
    
        int n=prices.length;
        int index=0;
        int max=0;
        for (int i = 0; i < n; i++) {
    
    
            if(prices[i]<prices[index]) index=i;
            if(prices[i]-prices[index]>max) max=prices[i]-prices[index];
        }
        return max;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43590593/article/details/112852141