forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.go
39 lines (33 loc) · 913 Bytes
/
Solution.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
type FreqStack struct {
cnt map[int]int
q hp
ts int
}
func Constructor() FreqStack {
return FreqStack{map[int]int{}, hp{}, 0}
}
func (this *FreqStack) Push(val int) {
this.cnt[val]++
this.ts++
heap.Push(&this.q, tuple{this.cnt[val], this.ts, val})
}
func (this *FreqStack) Pop() int {
val := heap.Pop(&this.q).(tuple).val
this.cnt[val]--
return val
}
type tuple struct{ cnt, ts, val int }
type hp []tuple
func (h hp) Len() int { return len(h) }
func (h hp) Less(i, j int) bool {
return h[i].cnt > h[j].cnt || h[i].cnt == h[j].cnt && h[i].ts > h[j].ts
}
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) }
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
/**
* Your FreqStack object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(val);
* param_2 := obj.Pop();
*/