-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathMoAlgorithm.js
More file actions
147 lines (129 loc) · 3.59 KB
/
MoAlgorithm.js
File metadata and controls
147 lines (129 loc) · 3.59 KB
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
147
/**
* Mo's Algorithm for answering range queries efficiently
* @module MoAlgorithm
*
* Convention: queries are [left, right) — right is exclusive.
*/
export class Query {
/**
* @param {number} left - inclusive
* @param {number} right - exclusive
* @param {number} index - original index of the query
*/
constructor(left, right, index) {
this.left = left
this.right = right
this.index = index
}
}
class QueryComparator {
constructor(blockSize) {
this.blockSize = blockSize
}
compare(a, b) {
const blockA = Math.floor(a.left / this.blockSize)
const blockB = Math.floor(b.left / this.blockSize)
if (blockA !== blockB) {
return blockA - blockB
}
return blockA % 2 === 0 ? a.right - b.right : b.right - a.right
}
}
export class MoAlgorithm {
/**
* @param {Array} arr
* @param {Array<Query>} queries
* @param {Function} addElement - called with arr[index]
* @param {Function} removeElement - called with arr[index]
* @param {Function} getResult - returns current result
*/
constructor(arr, queries, addElement, removeElement, getResult) {
this.arr = arr
this.queries = queries
this.addElement = addElement
this.removeElement = removeElement
this.getResult = getResult
this.blockSize = Math.max(1, Math.floor(Math.sqrt(arr.length)))
this.comparator = new QueryComparator(this.blockSize)
}
validateQueries() {
for (const q of this.queries) {
if (
!Number.isInteger(q.left) ||
!Number.isInteger(q.right) ||
q.left < 0 ||
q.right < 0 ||
q.left > q.right ||
q.right > this.arr.length
) {
throw new RangeError(
`Invalid query bounds: [${q.left}, ${q.right}) for array length ${this.arr.length}`
)
}
}
}
processQueries() {
// Validate input ranges first (right is allowed to be equal to arr.length)
this.validateQueries()
const sorted = [...this.queries].sort((a, b) =>
this.comparator.compare(a, b)
)
const results = new Array(this.queries.length)
let currentLeft = 0
let currentRight = -1 // current range is empty
for (const q of sorted) {
const left = q.left
const rightExclusive = q.right
const targetRight = rightExclusive - 1 // inclusive target index
// expand right
while (currentRight < targetRight) {
currentRight++
// safe: currentRight is guaranteed in [0, arr.length-1] by validation
this.addElement(this.arr[currentRight])
}
// move left leftwards (expand)
while (currentLeft > left) {
currentLeft--
this.addElement(this.arr[currentLeft])
}
// shrink right
while (currentRight > targetRight) {
this.removeElement(this.arr[currentRight])
currentRight--
}
// shrink left
while (currentLeft < left) {
this.removeElement(this.arr[currentLeft])
currentLeft++
}
results[q.index] = this.getResult()
}
return results
}
}
export class UniqueElementsCounter {
constructor() {
this.frequency = new Map()
this.uniqueCount = 0
}
addElement(element) {
const count = this.frequency.get(element) || 0
this.frequency.set(element, count + 1)
if (count === 0) {
this.uniqueCount++
}
}
removeElement(element) {
const count = this.frequency.get(element) || 0
if (count <= 0) return
if (count === 1) {
this.frequency.delete(element)
this.uniqueCount--
} else {
this.frequency.set(element, count - 1)
}
}
getResult() {
return this.uniqueCount
}
}