LeetCode:121. 买卖股票的最佳时机(单调栈)

class Solution {
    public int maxProfit(int[] prices) {
        int res=0;
        int MinPrice = Integer.MAX_VALUE;
        int len = prices.length-1;
        for(int i=0;i<=len;i++){
            if(MinPrice>prices[i]){
                MinPrice = prices[i];
            }
            else if((prices[i]-MinPrice)>res){
                res = prices[i]-MinPrice;
            }
        }

        return res;
    }
}

 

posted @ 2020-10-02 08:22  dlooooo  阅读(112)  评论(0编辑  收藏  举报