思路
一次循环,同时记录当前最大值,和全局最大值
code
func maxProfit(prices []int) int {
cur, max := 0, 0
for i := 1; i < len(prices); i++ {
cur = mymax(cur, cur+prices[i]-prices[i-1])
max = mymax(cur, max)
}
return max
}
func mymax(x, y int) int {
if x > y {
return x
}
return y
}
更多内容请移步我的repo:https://github.com/anakin/golang-leetcode