Skip to content

Commit 3642b0a

Browse files
authored
Create 1851-minimum-interval-to-include-each-query.js
1 parent b6c7eb3 commit 3642b0a

File tree

1 file changed

+302
-0
lines changed

1 file changed

+302
-0
lines changed
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
/**
2+
* @param {number[][]} intervals
3+
* @param {number[]} queries
4+
* @return {number[]}
5+
*/
6+
const minInterval = function (A, Q) {
7+
const QQ = []
8+
for (let idx = 0; idx < Q.length; idx++) QQ.push([Q[idx], idx])
9+
QQ.sort((a, b) => a[0] - b[0])
10+
A.sort((a, b) => a[0] - b[0])
11+
let i = 0,
12+
N = A.length
13+
const ans = Array(Q.length).fill(-1)
14+
const m = new TreeMap()
15+
const pq = new PriorityQueue((a, b) => a[0] < b[0])
16+
for (let [q, index] of QQ) {
17+
for (; i < N && A[i][0] <= q; i++) {
18+
let len = A[i][1] - A[i][0] + 1
19+
if (m.get(len) == null) m.set(len, 0)
20+
m.set(len, m.get(len) + 1)
21+
pq.push([A[i][1], len])
22+
}
23+
while (pq.size() > 0 && pq.peek()[0] < q) {
24+
let [right, len] = pq.peek()
25+
m.set(len, m.get(len) - 1)
26+
if (m.get(len) === 0) m.remove(len)
27+
pq.pop()
28+
}
29+
const first = m.getMinKey()
30+
if (m.getLength()) ans[index] = first
31+
}
32+
return ans
33+
}
34+
35+
class PriorityQueue {
36+
constructor(comparator = (a, b) => a > b) {
37+
this.heap = []
38+
this.top = 0
39+
this.comparator = comparator
40+
}
41+
size() {
42+
return this.heap.length
43+
}
44+
isEmpty() {
45+
return this.size() === 0
46+
}
47+
peek() {
48+
return this.heap[this.top]
49+
}
50+
push(...values) {
51+
values.forEach((value) => {
52+
this.heap.push(value)
53+
this.siftUp()
54+
})
55+
return this.size()
56+
}
57+
pop() {
58+
const poppedValue = this.peek()
59+
const bottom = this.size() - 1
60+
if (bottom > this.top) {
61+
this.swap(this.top, bottom)
62+
}
63+
this.heap.pop()
64+
this.siftDown()
65+
return poppedValue
66+
}
67+
replace(value) {
68+
const replacedValue = this.peek()
69+
this.heap[this.top] = value
70+
this.siftDown()
71+
return replacedValue
72+
}
73+
74+
parent = (i) => ((i + 1) >>> 1) - 1
75+
left = (i) => (i << 1) + 1
76+
right = (i) => (i + 1) << 1
77+
greater = (i, j) => this.comparator(this.heap[i], this.heap[j])
78+
swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]])
79+
siftUp = () => {
80+
let node = this.size() - 1
81+
while (node > this.top && this.greater(node, this.parent(node))) {
82+
this.swap(node, this.parent(node))
83+
node = this.parent(node)
84+
}
85+
}
86+
siftDown = () => {
87+
let node = this.top
88+
while (
89+
(this.left(node) < this.size() && this.greater(this.left(node), node)) ||
90+
(this.right(node) < this.size() && this.greater(this.right(node), node))
91+
) {
92+
let maxChild =
93+
this.right(node) < this.size() &&
94+
this.greater(this.right(node), this.left(node))
95+
? this.right(node)
96+
: this.left(node)
97+
this.swap(node, maxChild)
98+
node = maxChild
99+
}
100+
}
101+
}
102+
103+
function TreeMap() {
104+
var root = null
105+
var keyType = void 0
106+
var length = 0
107+
108+
return {
109+
each: each,
110+
set: set,
111+
get: get,
112+
getTree: getTree,
113+
getLength: getLength,
114+
getMaxKey: getMaxKey,
115+
getMinKey: getMinKey,
116+
remove: remove,
117+
}
118+
119+
function checkKey(key, checkKeyType) {
120+
var localKeyType = typeof key
121+
122+
if (
123+
localKeyType !== 'number' &&
124+
localKeyType !== 'string' &&
125+
localKeyType !== 'boolean'
126+
) {
127+
throw new Error("'key' must be a number, a string or a boolean")
128+
}
129+
130+
if (checkKeyType === true && localKeyType !== keyType) {
131+
throw new Error('All keys must be of the same type')
132+
}
133+
134+
return localKeyType
135+
}
136+
137+
function call(callback) {
138+
var args = Array.prototype.slice.call(arguments, 1)
139+
140+
if (typeof callback === 'function') {
141+
callback.apply(void 0, args)
142+
}
143+
}
144+
145+
function getTree() {
146+
return root
147+
}
148+
149+
function getLength() {
150+
return length
151+
}
152+
153+
function each(callback) {
154+
internalEach(root, callback)
155+
}
156+
157+
function internalEach(node, callback, internalCallback) {
158+
if (node === null) {
159+
return call(internalCallback)
160+
}
161+
162+
internalEach(node.left, callback, function () {
163+
call(callback, node.value, node.key)
164+
165+
internalEach(node.right, callback, function () {
166+
call(internalCallback)
167+
})
168+
})
169+
}
170+
171+
function get(key) {
172+
checkKey(key)
173+
174+
return internalGet(key, root)
175+
}
176+
177+
function internalGet(key, node) {
178+
if (node === null) {
179+
return void 0
180+
}
181+
182+
if (key < node.key) {
183+
return internalGet(key, node.left)
184+
} else if (key > node.key) {
185+
return internalGet(key, node.right)
186+
} else {
187+
return node.value
188+
}
189+
}
190+
191+
function set(key, value) {
192+
if (root === null) {
193+
keyType = checkKey(key)
194+
} else {
195+
checkKey(key, true)
196+
}
197+
198+
root = internalSet(key, value, root)
199+
}
200+
201+
function internalSet(key, value, node) {
202+
if (node === null) {
203+
length++
204+
205+
return {
206+
key: key,
207+
value: value,
208+
left: null,
209+
right: null,
210+
}
211+
}
212+
213+
if (key < node.key) {
214+
node.left = internalSet(key, value, node.left)
215+
} else if (key > node.key) {
216+
node.right = internalSet(key, value, node.right)
217+
} else {
218+
node.value = value
219+
}
220+
221+
return node
222+
}
223+
224+
function getMaxKey() {
225+
var maxNode = getMaxNode(root)
226+
227+
if (maxNode !== null) {
228+
return maxNode.key
229+
}
230+
231+
return maxNode
232+
}
233+
234+
function getMinKey() {
235+
var minNode = getMinNode(root)
236+
237+
if (minNode !== null) {
238+
return minNode.key
239+
}
240+
241+
return minNode
242+
}
243+
244+
function getMaxNode(node) {
245+
while (node !== null && node.right !== null) {
246+
node = node.right
247+
}
248+
249+
return node
250+
}
251+
252+
function getMinNode(node) {
253+
while (node !== null && node.left !== null) {
254+
node = node.left
255+
}
256+
257+
return node
258+
}
259+
260+
function remove(key) {
261+
checkKey(key)
262+
263+
root = internalRemove(key, root)
264+
}
265+
266+
function internalRemove(key, node) {
267+
if (node === null) {
268+
return null
269+
}
270+
271+
if (key < node.key) {
272+
node.left = internalRemove(key, node.left)
273+
} else if (key > node.key) {
274+
node.right = internalRemove(key, node.right)
275+
} else {
276+
if (node.left !== null && node.right !== null) {
277+
var maxNode = getMaxNode(node.left)
278+
279+
var maxNodeKey = maxNode.key
280+
var maxNodeValue = maxNode.value
281+
282+
maxNode.key = node.key
283+
maxNode.value = node.value
284+
node.key = maxNodeKey
285+
node.value = maxNodeValue
286+
287+
node.left = internalRemove(key, node.left)
288+
} else if (node.left !== null) {
289+
length--
290+
return node.left
291+
} else if (node.right !== null) {
292+
length--
293+
return node.right
294+
} else {
295+
length--
296+
return null
297+
}
298+
}
299+
300+
return node
301+
}
302+
}

0 commit comments

Comments
 (0)