-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdesign-most-recently-used-queue.go
93 lines (69 loc) · 1.53 KB
/
design-most-recently-used-queue.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
package main
import "sync"
type FenwickTree struct {
bitSums []int
mu sync.RWMutex
}
func NewFenwickTree(size int) *FenwickTree {
return &FenwickTree{
bitSums: make([]int, size + 1),
}
}
func leastSignificantBit(num int) int {
return num & (-num)
}
func (t *FenwickTree) Sum(pos int) int {
t.mu.RLock()
defer t.mu.RUnlock()
pos += 1
sum := 0
for pos > 0 {
sum += t.bitSums[pos]
pos -= leastSignificantBit(pos)
}
return sum
}
func (t *FenwickTree) Add(pos int, value int) {
t.mu.Lock()
defer t.mu.Unlock()
pos += 1
for pos < len(t.bitSums) {
t.bitSums[pos] += value
pos += leastSignificantBit(pos)
}
}
type MRUQueue struct {
fenwickTree *FenwickTree
elements []int
}
func Constructor(n int) MRUQueue {
q := MRUQueue{
fenwickTree: NewFenwickTree(n + 2000 + 1),
elements: make([]int, n),
}
for i := range make([]bool, n) {
q.elements[i] = i + 1
q.fenwickTree.Add(i, 1)
}
return q
}
func (this *MRUQueue) Fetch(k int) int {
// Search where the kth element is in the sparse array
left, right := 0, len(this.elements)
for left < right {
mid := left + (right - left) / 2
if this.fenwickTree.Sum(mid) < k {
left = mid + 1
} else {
right = mid
}
}
pos := left
// Remove element from the current pos (no need to actually remove it,
// we'll never access it again)
this.fenwickTree.Add(pos, -1)
// Extend the array and move element to the end
this.elements = append(this.elements, this.elements[pos])
this.fenwickTree.Add(len(this.elements) - 1, 1)
return this.elements[pos]
}