版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412844
思路
一次循环,同时记录当前最大值,和全局最大值
code
代码语言:javascript复制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
}