-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0121.go
51 lines (42 loc) · 894 Bytes
/
0121.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
func maxProfit(prices []int) int {
if len(prices) == 0 || len(prices) == 1 {
return 0
}
profits := make([]int, len(prices))
profits[0] = 0
maxProfit := profits[0]
minPrice := prices[0]
for n := 1; n < len(prices); n++ {
profits[n] = getTwoMaxNumber(profits[n-1], prices[n]-minPrice)
if profits[n] > maxProfit {
maxProfit = profits[n]
}
minPrice = getTwoMinNumber(minPrice, prices[n])
}
return maxProfit
}
func getTwoMaxNumber(n1 int, n2 int) int {
if n1 > n2 {
return n1
} else {
return n2
}
}
func getTwoMinNumber(n1 int, n2 int) int {
if n1 > n2 {
return n2
} else {
return n1
}
}
func getMaxProfit(n int, curPrice int, prices []int) int {
maxProfit := curPrice - prices[0]
for index := 0; index < n; index++ {
curProfit := curPrice - prices[index]
if curProfit > maxProfit {
maxProfit = curProfit
}
}
return maxProfit
}