Skip to content

Commit a07264d

Browse files
authored
Update 1942-the-number-of-the-smallest-unoccupied-chair.js
1 parent 5848599 commit a07264d

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

1942-the-number-of-the-smallest-unoccupied-chair.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,106 @@
1+
/**
2+
* @param {number[][]} times
3+
* @param {number} targetFriend
4+
* @return {number}
5+
*/
6+
const smallestChair = function(times, targetFriend) {
7+
times.forEach((e, i) => e[2] = i)
8+
times.sort((a, b) => a[0] - b[0])
9+
const pq = new PQ((a, b) => a[1] < b[1])
10+
const available = new PQ((a, b) => a < b)
11+
const n = times.length
12+
for(let i = 0; i < n; i++) {
13+
available.push(i)
14+
}
15+
const seat = available.pop()
16+
times[0].push(seat)
17+
pq.push(times[0])
18+
if(times[0][2] === targetFriend) return seat
19+
for(let i = 1; i < n; i++) {
20+
const el = times[i]
21+
const [s, e, idx] = el
22+
while(!pq.isEmpty() && pq.peek()[1] <= s) {
23+
const tmp = pq.pop()
24+
available.push(tmp[3])
25+
}
26+
const seat = available.pop()
27+
if(targetFriend === idx) return seat
28+
el.push(seat)
29+
pq.push(el)
30+
}
31+
32+
};
33+
34+
class PQ {
35+
constructor(comparator = (a, b) => a > b) {
36+
this.heap = []
37+
this.top = 0
38+
this.comparator = comparator
39+
}
40+
size() {
41+
return this.heap.length
42+
}
43+
isEmpty() {
44+
return this.size() === 0
45+
}
46+
peek() {
47+
return this.heap[this.top]
48+
}
49+
push(...values) {
50+
values.forEach((value) => {
51+
this.heap.push(value)
52+
this.siftUp()
53+
})
54+
return this.size()
55+
}
56+
pop() {
57+
const poppedValue = this.peek()
58+
const bottom = this.size() - 1
59+
if (bottom > this.top) {
60+
this.swap(this.top, bottom)
61+
}
62+
this.heap.pop()
63+
this.siftDown()
64+
return poppedValue
65+
}
66+
replace(value) {
67+
const replacedValue = this.peek()
68+
this.heap[this.top] = value
69+
this.siftDown()
70+
return replacedValue
71+
}
72+
73+
parent = (i) => ((i + 1) >>> 1) - 1
74+
left = (i) => (i << 1) + 1
75+
right = (i) => (i + 1) << 1
76+
greater = (i, j) => this.comparator(this.heap[i], this.heap[j])
77+
swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]])
78+
siftUp = () => {
79+
let node = this.size() - 1
80+
while (node > this.top && this.greater(node, this.parent(node))) {
81+
this.swap(node, this.parent(node))
82+
node = this.parent(node)
83+
}
84+
}
85+
siftDown = () => {
86+
let node = this.top
87+
while (
88+
(this.left(node) < this.size() && this.greater(this.left(node), node)) ||
89+
(this.right(node) < this.size() && this.greater(this.right(node), node))
90+
) {
91+
let maxChild =
92+
this.right(node) < this.size() &&
93+
this.greater(this.right(node), this.left(node))
94+
? this.right(node)
95+
: this.left(node)
96+
this.swap(node, maxChild)
97+
node = maxChild
98+
}
99+
}
100+
}
101+
102+
// another
103+
1104
/**
2105
* @param {number[][]} times
3106
* @param {number} targetFriend

0 commit comments

Comments
 (0)