-
Notifications
You must be signed in to change notification settings - Fork 1
/
winner.go
96 lines (91 loc) · 1.74 KB
/
winner.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package winner
import "github.com/catorpilor/leetcode/utils"
func winnerOfGame(arr []int, k int) int {
return useTwoPassWithStack(arr, k)
}
// useStack time complexity O(N), space complexity O(N)
func useTwoPassWithStack(arr []int, k int) int {
n := len(arr)
posOfPeeks := make([]int, 0, n)
var maxV int
for i := 0; i < n; i++ {
if i > 0 && arr[i] < arr[i-1] {
continue
}
if i < n-1 && arr[i] < arr[i+1] {
continue
}
posOfPeeks = append(posOfPeeks, i)
maxV = utils.Max(maxV, arr[i])
}
// len(posOfPeeks) means the arr is strictly increasing or decreasing
if len(posOfPeeks) == 1 || n < k {
return maxV
}
st := make([]int, 0, n)
count := make([]int, n)
for i := 0; i < n; i++ {
nst := len(st)
if nst > 0 {
top := st[nst-1]
if arr[top] > arr[i] {
count[top]++
if count[top] >= k {
return arr[top]
}
continue
} else {
st = st[:nst-1]
nst--
count[i]++
}
}
st = append(st, i)
}
return maxV
}
// useOnePassWithStack time complexity O(N), space complexity O(N)
func useOnePassWithStack(arr []int, k int) int {
n := len(arr)
st := make([]int, 0, n)
count := make([]int, n)
for i := 0; i < n; i++ {
nst := len(st)
if nst > 0 {
top := st[nst-1]
if arr[top] > arr[i] {
count[top]++
if count[top] >= k {
return arr[top]
}
continue
} else {
st = st[:nst-1]
nst--
count[i]++
if count[i] >= k {
return arr[i]
}
}
}
st = append(st, i)
}
return arr[st[0]]
}
// useOnePass time complexity O(N), space complexity O(1)
func useOnePass(arr []int, k int) int {
n := len(arr)
cur := arr[0]
wins := 0
for i := 1; i < n; i++ {
if arr[i] > cur {
cur = arr[i]
wins = 0
}
wins++
if wins >= k {
break
}
}
return cur
}