双指针遍历/滑动窗口 —— 121_买卖股票的最佳时机

7. 121_买卖股票的最佳时机
/*
输入: [7,1,5,3,6,4]
输出: 5
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
     注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
*/
class Solution {
    public int maxProfit(int[] prices) {
        if(prices == null || prices.length <= 1) return 0;
        int max = 0;
        int min = prices[0];
        for(int i=1; i < prices.length; ++i){
            if((prices[i] - min) > max) max = prices[i] - min;
            if(prices[i] < min) min = prices[i];
        }
        return max;
    }
}
原文地址:https://www.cnblogs.com/s841844054/p/13736361.html