LeetCode题解-121(Golang实现)
题目:
121. Best Time to Buy and Sell Stock
解答:
func maxProfit(prices []int) int {
var minB, maxP int
for i := 0; i < len(prices); i++ {
if i > 0 {
if prices[i]-minB > maxP {
maxP = prices[i] - minB
}
if prices[i] < minB {
minB = prices[i]
}
} else {
minB = prices[i]
maxP = 0
}
}
return maxP
}