leetcode-121

暴力法就是两次迭代,对于这道题倒是ok,但是实际场景是不能选择的,因此直接用单步迭代的方式,无非就是记录最低点,找max差值点。

func maxProfit(prices []int) int {
    var minprice = math.MaxInt64
    var maxprofit = 0

    for i := 0; i < len(prices); i++ {
        if prices[i] < minprice {
            minprice = prices[i]
        } else if prices[i]-minprice > maxprofit {
            maxprofit = prices[i] - minprice
        }
    }
    return maxprofit
}

end

一个没有高级趣味的人。 email:hushui502@gmail.com
原文地址:https://www.cnblogs.com/CherryTab/p/12452662.html