This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
all-oone-data-structure.go
executable file
·146 lines (125 loc) · 2.93 KB
/
all-oone-data-structure.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package problem0432
import "container/heap"
// AllOne 是解题所需的结构
type AllOne struct {
m map[string]*entry
// 利用优先队列来维护 拥有极值的 key
max maxPQ
min minPQ
}
// Constructor initialize your data structure here.
func Constructor() AllOne {
return AllOne{m: make(map[string]*entry),
max: maxPQ{},
min: minPQ{},
}
}
// Inc inserts a new key <Key> with value 1. Or increments an existing key by 1.
func (a *AllOne) Inc(key string) {
if ep, ok := a.m[key]; ok {
ep.value++
heap.Fix(&a.max, ep.maxIndex)
heap.Fix(&a.min, ep.maxIndex)
} else {
ep = &entry{key: key, value: 1}
a.m[key] = ep
heap.Push(&a.max, ep)
heap.Push(&a.min, ep)
}
}
// Dec decrements an existing key by 1. If Key's value is 1, remove it from the data structure.
func (a *AllOne) Dec(key string) {
if ep, ok := a.m[key]; !ok {
return
} else if ep.value == 1 {
heap.Remove(&a.max, ep.maxIndex)
heap.Remove(&a.min, ep.minIndex)
} else {
ep.value--
heap.Fix(&a.max, ep.maxIndex)
heap.Fix(&a.min, ep.minIndex)
}
}
// GetMaxKey returns one of the keys with maximal value.
func (a *AllOne) GetMaxKey() string {
if len(a.max) == 0 {
return ""
}
return a.max[0].key
}
// GetMinKey returns one of the keys with Minimal value.
func (a *AllOne) GetMinKey() string {
if len(a.min) == 0 {
return ""
}
return a.min[0].key
}
/**
* Your AllOne object will be instantiated and called as such:
* obj := Constructor();
* obj.Inc(key);
* obj.Dec(key);
* param_3 := obj.GetMaxKey();
* param_4 := obj.GetMinKey();
*/
// entry 是 priorityQueue 中的元素
type entry struct {
key string
value int
maxIndex int
minIndex int
}
// priorityQueue implements heap.Interface and holds entrys.
type minPQ []*entry
func (pq minPQ) Len() int { return len(pq) }
func (pq minPQ) Less(i, j int) bool {
return pq[i].value < pq[j].value
}
func (pq minPQ) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].minIndex = i
pq[j].minIndex = j
}
// Push 往 pq 中放 entry
func (pq *minPQ) Push(x interface{}) {
n := len(*pq)
entry := x.(*entry)
entry.minIndex = n
*pq = append(*pq, entry)
}
// Pop 从 pq 中取出最优先的 entry
func (pq *minPQ) Pop() interface{} {
old := *pq
n := len(old)
entry := old[n-1]
entry.minIndex = -1 // for safety
*pq = old[0 : n-1]
return entry
}
// priorityQueue implements heap.Interface and holds entrys.
type maxPQ []*entry
func (pq maxPQ) Len() int { return len(pq) }
func (pq maxPQ) Less(i, j int) bool {
return pq[i].value > pq[j].value
}
func (pq maxPQ) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].maxIndex = i
pq[j].maxIndex = j
}
// Push 往 pq 中放 entry
func (pq *maxPQ) Push(x interface{}) {
n := len(*pq)
entry := x.(*entry)
entry.maxIndex = n
*pq = append(*pq, entry)
}
// Pop 从 pq 中取出最优先的 entry
func (pq *maxPQ) Pop() interface{} {
old := *pq
n := len(old)
entry := old[n-1]
entry.maxIndex = -1 // for safety
*pq = old[0 : n-1]
return entry
}