leet-code/best-time-to-buy-and-sell-s.../sol.go

16 lines
251 B
Go

package main
// Time: O(n)
// Space: O(1)
func maxProfit(prices []int) int {
min, max := prices[0], 0
for _, price := range prices {
if price < min {
min = price
} else if diff := price - min; diff > max {
max = diff
}
}
return max
}