美文网首页
121. Best Time to Buy and Sell S

121. Best Time to Buy and Sell S

作者: 沉睡至夏 | 来源:发表于2016-12-18 03:09 被阅读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;
    }
}

相关文章

网友评论

      本文标题:121. Best Time to Buy and Sell S

      本文链接:https://www.haomeiwen.com/subject/appamttx.html