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

题目链接:LeeCode121买卖股票的最佳时机
题目描述:LeeCode121买卖股票的最佳时机(Java)(双指针)_第1张图片
当前天最大盈利是今天价格减之前天里最小的价格,依次找

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

你可能感兴趣的:(leecode,leetcode,数据结构,算法,java,指针)