We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8678b9a commit 59f2aedCopy full SHA for 59f2aed
best-time-to-buy-and-sell-stock/invidam.go
@@ -1,12 +1,10 @@
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
- }
+ purchasePrice := prices[0]
+ maxBenefit := 0
6
7
- maxPriceDiff := 0
8
- for i, price := range prices {
9
- maxPriceDiff = max(maxPriceDiff, maxPriceFrom[i+1]-price)
+ for _, price := range prices {
+ purchasePrice = min(purchasePrice, price)
+ maxBenefit = max(maxBenefit, price-purchasePrice)
10
}
11
- return maxPriceDiff
+ return maxBenefit
12
0 commit comments