-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
33 lines (28 loc) · 786 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main
func maxProfit(prices []int) int {
// just a basic check for the correct input
if len(prices) < 2 {
return 0
}
// here we're going to store the max profit
var bestProfit int
// my current best price if I buy on the first day
bestPrice := prices[0]
// we loop over all prices except the first day as we just bought the stock
for i := 1; i < len(prices); i++ {
p := prices[i]
// it would be nice we found cheaper price, so lets buy this day instead
if p < bestPrice {
bestPrice = p
} else {
// if we reached the end of the list of next day price is going to drop low
if i == len(prices)-1 || prices[i+1] < p {
// sell and calc the profit
bestProfit += p - bestPrice
// buy again
bestPrice = p
}
}
}
return bestProfit
}