best-time-to-buy-and-sell-stock
This commit is contained in:
parent
8c338a768f
commit
b2132c4a69
|
@ -0,0 +1,15 @@
|
||||||
|
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
|
||||||
|
}
|
Loading…
Reference in New Issue