-
Notifications
You must be signed in to change notification settings - Fork 0
/
121.go
53 lines (46 loc) · 1008 Bytes
/
121.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
52
53
/*
# ------------------------------ #
# #
# version 0.0.1 #
# #
# Aleksiej Ostrowski, 2022 #
# #
# https://aleksiej.com #
# #
# ------------------------------ #
*/
package main
import (
"fmt"
)
func max(a, b int) int {
if a > b {
return a
} else {
return b
}
}
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/solutions/1735550/python-javascript-easy-solution-with-very-clear-explanation/
func maxProfit(prices []int) int {
left := 0
right := 1
max_profit := 0
len_prices := len(prices)
for {
if right >= len_prices {
break
}
currentProfit := prices[right] - prices[left]
if prices[left] < prices[right] {
max_profit = max(currentProfit, max_profit)
} else {
left = right
}
right += 1
}
return max_profit
}
func main() {
prices := []int{7, 1, 5, 3, 6, 4}
fmt.Println(maxProfit(prices))
}