Leetcode Golang 122. Best Time to Buy and Sell Stock II.go

2019-04-12 10:26:40 浏览数 (1)

版权声明:原创勿转 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
}

0 人点赞