121. 買賣股票的最佳時機

gdut17_2發表於2024-06-07
func maxProfit(prices []int) int {

    max := 0
    left := 0
    for i:=0; i<len(prices); i++ {
        dif := prices[i]-prices[left]
        if dif > max {
            max = dif
        }
        if dif < 0 {
            left = i 
        }
    }
    return max
}

相關文章