Skip to content

Commit 8678b9a

Browse files
committed
best-time-to-buy-and-sell-stock: solve problem
1 parent 555ea05 commit 8678b9a

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func maxProfit(prices []int) int {
2+
maxPriceFrom := make([]int, len(prices)+1)
3+
for i := len(prices) - 1; i >= 0; i-- {
4+
maxPriceFrom[i] = max(maxPriceFrom[i+1], prices[i])
5+
}
6+
7+
maxPriceDiff := 0
8+
for i, price := range prices {
9+
maxPriceDiff = max(maxPriceDiff, maxPriceFrom[i+1]-price)
10+
}
11+
return maxPriceDiff
12+
}

0 commit comments

Comments
 (0)