Skip to content

Commit a4e6944

Browse files
authored
best time to buy and sell stock solution
1 parent 4f5c769 commit a4e6944

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func maxProfit(prices []int) int {
2+
ans := 0
3+
bought := prices[0]
4+
for i := 1; i != len(prices); i++ {
5+
ans = max(ans, prices[i] - bought)
6+
bought = min(bought, prices[i])
7+
}
8+
return ans
9+
}

0 commit comments

Comments
 (0)