Skip to content

Commit d743878

Browse files
committed
refactor Best Time to Buy and Sell Stock
1 parent d6e6e0b commit d743878

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

go/best_time_to_buy_and_sell_stock.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
package main
33

44
func maxProfit(prices []int) int {
5-
lowestPrice, highestProfit := prices[0], 0
6-
for _, n := range prices {
7-
lowestPrice = min(lowestPrice, n)
8-
highestProfit = max(highestProfit, n-lowestPrice)
5+
minPrice, maxProfit := prices[0], 0
6+
for _, price := range prices {
7+
minPrice = min(minPrice, price)
8+
maxProfit = max(maxProfit, price-minPrice)
99
}
10-
return highestProfit
10+
return maxProfit
1111
}

0 commit comments

Comments
 (0)