-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (63 loc) · 1.06 KB
/
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
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
package main
type MaxPQ struct {
pq []int
n int
}
func (m *MaxPQ) max() int {
if len(m.pq) <= 1 {
return -1
}
return m.pq[1]
}
func (m *MaxPQ) insert(n int) {
}
func (m *MaxPQ) delMax() int {
if len(m.pq) <= 1 {
return -1
}
return m.pq[1]
}
func (m *MaxPQ) swim(k int) {
for k > 1 && m.less(m.parent(k), k) {
m.exchange(m.parent(k), k)
k = m.parent(k)
}
}
func (m *MaxPQ) less(i int, j int) bool {
return m.pq[i] < m.pq[j]
}
func (m *MaxPQ) sink(k int) {
for m.left(k) <= len(m.pq) {
olderIndex := m.left(k)
if m.right(k) <= len(m.pq) && m.less(m.left(k), m.right(k)) {
olderIndex = m.right(k)
}
if m.less(olderIndex, k) {
break
}
m.exchange(olderIndex, k)
k = olderIndex
}
}
func (m *MaxPQ) exchange(i int, j int) {
temp := m.pq[i]
m.pq[i] = m.pq[j]
m.pq[j] = temp
}
func newMaxPQ() *MaxPQ {
return &MaxPQ{
pq: []int{-1},
n: 0,
}
}
func (m *MaxPQ) parent(root int) int {
return root / 2
}
func (m *MaxPQ) left(root int) int {
return root * 2
}
func (m *MaxPQ) right(root int) int {
return root*2 + 1
}
func main() {
}