Skip to content

Commit ed5899d

Browse files
authored
Merge pull request neetcode-gh#555 from MaratKhakim/121_best_time_to_buy_and_sell
Go: 121 Best Time to Buy and Sell Stock
2 parents 35514f0 + 1d8cfb2 commit ed5899d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func maxProfit(prices []int) int {
2+
min := math.MaxUint32
3+
res := 0
4+
5+
for _, price := range prices {
6+
if price > min {
7+
if price-min > res {
8+
res = price - min
9+
}
10+
} else {
11+
min = price
12+
}
13+
}
14+
15+
return res
16+
}

0 commit comments

Comments
 (0)