121. Best Time to Buy and Sell S

2016-12-18  本文已影响5人  沉睡至夏

maintain the "max_profit" and "min_price".

public class Solution {
    public int max_profit(int[] prices) {
        int min_price = Integer.MAX_VALUE;
        int max_pro = 0;
        for (int i=0; i<prices.length; i++) {
            min_price = Math.min(min_price, prices[i]);
            max_pro = Math.max(max_pro, prices[i]-min_price);
        }
        return max_pro;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读