Skip to content

Instantly share code, notes, and snippets.

@MorrisLaw
Last active June 3, 2022 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MorrisLaw/f2063edc2323e1274f59600f17f782d6 to your computer and use it in GitHub Desktop.
Save MorrisLaw/f2063edc2323e1274f59600f17f782d6 to your computer and use it in GitHub Desktop.
Best Time to Buy and Sell Stock - LeetCode 121
func maxProfit(prices []int) int {
var max int
l, r := 0, 1
for r < len(prices) {
if prices[l] < prices[r] {
max = int(math.Max(float64(max), float64(prices[r]-prices[l])))
} else {
l = r
}
r++
}
return max
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment