From d8a43961ac034bd1ef35bc3958e35b49e8cb2fc1 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 6 Feb 2025 19:35:34 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1942 No.1942.The Number of the Smallest Unoccupied Chair --- .../README.md | 181 +++++++++++++---- .../README_EN.md | 185 ++++++++++++++---- .../Solution.cpp | 29 +-- .../Solution.go | 41 ++++ .../Solution.java | 27 ++- .../Solution.js | 25 +++ .../Solution.py | 17 +- .../Solution.ts | 21 ++ 8 files changed, 415 insertions(+), 111 deletions(-) create mode 100644 solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.go create mode 100644 solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.js create mode 100644 solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.ts diff --git a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/README.md b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/README.md index c1d953feb2e75..c205a3e0dd186 100644 --- a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/README.md +++ b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/README.md @@ -80,7 +80,15 @@ tags: -### 方法一:优先队列(最小堆) +### 方法一:优先队列(小根堆) + +我们首先将每个朋友的到达时间、离开时间和编号组成一个三元组,然后按到达时间排序。 + +我们使用一个小根堆 $\textit{idle}$ 来存储当前空闲的椅子编号,初始时,我们将 $0, 1, \ldots, n-1$ 加入 $\textit{idle}$ 中。使用一个小根堆 $\textit{busy}$ 存储二元组 $(\textit{leaving}, \textit{chair})$,其中 $\textit{leaving}$ 表示离开时间,而 $\textit{chair}$ 表示椅子编号。 + +遍历每个朋友的到达时间、离开时间和编号,对于每个朋友,我们首先将所有离开时间小于等于当前朋友到达时间的朋友从 $\textit{busy}$ 中弹出,将他们占据的椅子编号加入 $\textit{idle}$ 中。然后我们从 $\textit{idle}$ 中弹出一个椅子编号,将其分配给当前朋友,将 $(\textit{leaving}, \textit{chair})$ 加入 $\textit{busy}$ 中。如果当前朋友的编号等于 $\textit{targetFriend}$,我们返回当前分配的椅子编号。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为朋友的个数。 @@ -90,20 +98,19 @@ tags: class Solution: def smallestChair(self, times: List[List[int]], targetFriend: int) -> int: n = len(times) - h = list(range(n)) - heapify(h) for i in range(n): times[i].append(i) times.sort() + idle = list(range(n)) + heapify(idle) busy = [] - for a, b, i in times: - while busy and busy[0][0] <= a: - heappush(h, heappop(busy)[1]) - c = heappop(h) + for arrival, leaving, i in times: + while busy and busy[0][0] <= arrival: + heappush(idle, heappop(busy)[1]) + j = heappop(idle) if i == targetFriend: - return c - heappush(busy, (b, c)) - return -1 + return j + heappush(busy, (leaving, j)) ``` #### Java @@ -112,24 +119,23 @@ class Solution: class Solution { public int smallestChair(int[][] times, int targetFriend) { int n = times.length; - int[][] ts = new int[n][3]; - PriorityQueue q = new PriorityQueue<>(); - PriorityQueue busy = new PriorityQueue<>((a, b) -> a[0] - b[0]); + PriorityQueue idle = new PriorityQueue<>(); + PriorityQueue busy = new PriorityQueue<>(Comparator.comparingInt(a -> a[0])); for (int i = 0; i < n; ++i) { - ts[i] = new int[] {times[i][0], times[i][1], i}; - q.offer(i); + times[i] = new int[] {times[i][0], times[i][1], i}; + idle.offer(i); } - Arrays.sort(ts, (a, b) -> a[0] - b[0]); - for (int[] t : ts) { - int a = t[0], b = t[1], i = t[2]; - while (!busy.isEmpty() && busy.peek()[0] <= a) { - q.offer(busy.poll()[1]); + Arrays.sort(times, Comparator.comparingInt(a -> a[0])); + for (var e : times) { + int arrival = e[0], leaving = e[1], i = e[2]; + while (!busy.isEmpty() && busy.peek()[0] <= arrival) { + idle.offer(busy.poll()[1]); } - int c = q.poll(); + int j = idle.poll(); if (i == targetFriend) { - return c; + return j; } - busy.offer(new int[] {b, c}); + busy.offer(new int[] {leaving, j}); } return -1; } @@ -139,35 +145,138 @@ class Solution { #### C++ ```cpp -using pii = pair; - class Solution { public: int smallestChair(vector>& times, int targetFriend) { - priority_queue, greater> q; + using pii = pair; priority_queue, greater> busy; + priority_queue, greater> idle; int n = times.size(); for (int i = 0; i < n; ++i) { times[i].push_back(i); - q.push(i); + idle.push(i); } - sort(times.begin(), times.end()); - for (auto& t : times) { - int a = t[0], b = t[1], i = t[2]; - while (!busy.empty() && busy.top().first <= a) { - q.push(busy.top().second); + ranges::sort(times); + for (const auto& e : times) { + int arrival = e[0], leaving = e[1], i = e[2]; + while (!busy.empty() && busy.top().first <= arrival) { + idle.push(busy.top().second); busy.pop(); } - int c = q.top(); - q.pop(); - if (i == targetFriend) return c; - busy.push({b, c}); + int j = idle.top(); + if (i == targetFriend) { + return j; + } + idle.pop(); + busy.emplace(leaving, j); } return -1; } }; ``` +#### Go + +```go +func smallestChair(times [][]int, targetFriend int) int { + idle := hp{} + busy := hp2{} + for i := range times { + times[i] = append(times[i], i) + heap.Push(&idle, i) + } + sort.Slice(times, func(i, j int) bool { return times[i][0] < times[j][0] }) + for _, e := range times { + arrival, leaving, i := e[0], e[1], e[2] + for len(busy) > 0 && busy[0].t <= arrival { + heap.Push(&idle, heap.Pop(&busy).(pair).i) + } + j := heap.Pop(&idle).(int) + if i == targetFriend { + return j + } + heap.Push(&busy, pair{leaving, j}) + } + return -1 +} + +type hp struct{ sort.IntSlice } + +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { + a := h.IntSlice + v := a[len(a)-1] + h.IntSlice = a[:len(a)-1] + return v +} + +type pair struct{ t, i int } +type hp2 []pair + +func (h hp2) Len() int { return len(h) } +func (h hp2) Less(i, j int) bool { return h[i].t < h[j].t || (h[i].t == h[j].t && h[i].i < h[j].i) } +func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp2) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp2) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +``` + +#### TypeScript + +```ts +function smallestChair(times: number[][], targetFriend: number): number { + const n = times.length; + const idle = new MinPriorityQueue(); + const busy = new MinPriorityQueue({ priority: v => v[0] }); + for (let i = 0; i < n; ++i) { + times[i].push(i); + idle.enqueue(i); + } + times.sort((a, b) => a[0] - b[0]); + for (const [arrival, leaving, i] of times) { + while (busy.size() > 0 && busy.front().element[0] <= arrival) { + idle.enqueue(busy.dequeue().element[1]); + } + const j = idle.dequeue().element; + if (i === targetFriend) { + return j; + } + busy.enqueue([leaving, j]); + } + return -1; +} +``` + +#### JavaScript + +```js +/** + * @param {number[][]} times + * @param {number} targetFriend + * @return {number} + */ +var smallestChair = function (times, targetFriend) { + const n = times.length; + const idle = new MinPriorityQueue(); + const busy = new MinPriorityQueue({ priority: v => v[0] }); + for (let i = 0; i < n; ++i) { + times[i].push(i); + idle.enqueue(i); + } + times.sort((a, b) => a[0] - b[0]); + for (const [arrival, leaving, i] of times) { + while (busy.size() > 0 && busy.front().element[0] <= arrival) { + idle.enqueue(busy.dequeue().element[1]); + } + const j = idle.dequeue().element; + if (i === targetFriend) { + return j; + } + busy.enqueue([leaving, j]); + } +}; +``` + diff --git a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/README_EN.md b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/README_EN.md index ec4b0aabf85cf..cc45e04b9f3d9 100644 --- a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/README_EN.md +++ b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/README_EN.md @@ -38,7 +38,7 @@ tags:
 Input: times = [[1,4],[2,3],[4,6]], targetFriend = 1
 Output: 1
-Explanation: 
+Explanation:
 - Friend 0 arrives at time 1 and sits on chair 0.
 - Friend 1 arrives at time 2 and sits on chair 1.
 - Friend 1 leaves at time 3 and chair 1 becomes empty.
@@ -52,7 +52,7 @@ Since friend 1 sat on chair 1, we return 1.
 
 Input: times = [[3,10],[1,5],[2,6]], targetFriend = 0
 Output: 2
-Explanation: 
+Explanation:
 - Friend 1 arrives at time 1 and sits on chair 0.
 - Friend 2 arrives at time 2 and sits on chair 1.
 - Friend 0 arrives at time 3 and sits on chair 2.
@@ -80,7 +80,15 @@ Since friend 0 sat on chair 2, we return 2.
 
 
 
-### Solution 1
+### Solution 1: Priority Queue (Min-Heap)
+
+First, we create a tuple for each friend consisting of their arrival time, leaving time, and index, then sort these tuples by arrival time.
+
+We use a min-heap $\textit{idle}$ to store the currently available chair numbers. Initially, we add $0, 1, \ldots, n-1$ to $\textit{idle}$. We also use a min-heap $\textit{busy}$ to store tuples $(\textit{leaving}, \textit{chair})$, where $\textit{leaving}$ represents the leaving time and $\textit{chair}$ represents the chair number.
+
+We iterate through each friend's arrival time, leaving time, and index. For each friend, we first remove all friends from $\textit{busy}$ whose leaving time is less than or equal to the current friend's arrival time, and add their chair numbers back to $\textit{idle}$. Then we pop a chair number from $\textit{idle}$, assign it to the current friend, and add $(\textit{leaving}, \textit{chair})$ to $\textit{busy}$. If the current friend's index is equal to $\textit{targetFriend}$, we return the assigned chair number.
+
+The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of friends.
 
 
 
@@ -90,20 +98,19 @@ Since friend 0 sat on chair 2, we return 2.
 class Solution:
     def smallestChair(self, times: List[List[int]], targetFriend: int) -> int:
         n = len(times)
-        h = list(range(n))
-        heapify(h)
         for i in range(n):
             times[i].append(i)
         times.sort()
+        idle = list(range(n))
+        heapify(idle)
         busy = []
-        for a, b, i in times:
-            while busy and busy[0][0] <= a:
-                heappush(h, heappop(busy)[1])
-            c = heappop(h)
+        for arrival, leaving, i in times:
+            while busy and busy[0][0] <= arrival:
+                heappush(idle, heappop(busy)[1])
+            j = heappop(idle)
             if i == targetFriend:
-                return c
-            heappush(busy, (b, c))
-        return -1
+                return j
+            heappush(busy, (leaving, j))
 ```
 
 #### Java
@@ -112,24 +119,23 @@ class Solution:
 class Solution {
     public int smallestChair(int[][] times, int targetFriend) {
         int n = times.length;
-        int[][] ts = new int[n][3];
-        PriorityQueue q = new PriorityQueue<>();
-        PriorityQueue busy = new PriorityQueue<>((a, b) -> a[0] - b[0]);
+        PriorityQueue idle = new PriorityQueue<>();
+        PriorityQueue busy = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
         for (int i = 0; i < n; ++i) {
-            ts[i] = new int[] {times[i][0], times[i][1], i};
-            q.offer(i);
+            times[i] = new int[] {times[i][0], times[i][1], i};
+            idle.offer(i);
         }
-        Arrays.sort(ts, (a, b) -> a[0] - b[0]);
-        for (int[] t : ts) {
-            int a = t[0], b = t[1], i = t[2];
-            while (!busy.isEmpty() && busy.peek()[0] <= a) {
-                q.offer(busy.poll()[1]);
+        Arrays.sort(times, Comparator.comparingInt(a -> a[0]));
+        for (var e : times) {
+            int arrival = e[0], leaving = e[1], i = e[2];
+            while (!busy.isEmpty() && busy.peek()[0] <= arrival) {
+                idle.offer(busy.poll()[1]);
             }
-            int c = q.poll();
+            int j = idle.poll();
             if (i == targetFriend) {
-                return c;
+                return j;
             }
-            busy.offer(new int[] {b, c});
+            busy.offer(new int[] {leaving, j});
         }
         return -1;
     }
@@ -139,35 +145,138 @@ class Solution {
 #### C++
 
 ```cpp
-using pii = pair;
-
 class Solution {
 public:
     int smallestChair(vector>& times, int targetFriend) {
-        priority_queue, greater> q;
+        using pii = pair;
         priority_queue, greater> busy;
+        priority_queue, greater> idle;
         int n = times.size();
         for (int i = 0; i < n; ++i) {
             times[i].push_back(i);
-            q.push(i);
+            idle.push(i);
         }
-        sort(times.begin(), times.end());
-        for (auto& t : times) {
-            int a = t[0], b = t[1], i = t[2];
-            while (!busy.empty() && busy.top().first <= a) {
-                q.push(busy.top().second);
+        ranges::sort(times);
+        for (const auto& e : times) {
+            int arrival = e[0], leaving = e[1], i = e[2];
+            while (!busy.empty() && busy.top().first <= arrival) {
+                idle.push(busy.top().second);
                 busy.pop();
             }
-            int c = q.top();
-            q.pop();
-            if (i == targetFriend) return c;
-            busy.push({b, c});
+            int j = idle.top();
+            if (i == targetFriend) {
+                return j;
+            }
+            idle.pop();
+            busy.emplace(leaving, j);
         }
         return -1;
     }
 };
 ```
 
+#### Go
+
+```go
+func smallestChair(times [][]int, targetFriend int) int {
+	idle := hp{}
+	busy := hp2{}
+	for i := range times {
+		times[i] = append(times[i], i)
+		heap.Push(&idle, i)
+	}
+	sort.Slice(times, func(i, j int) bool { return times[i][0] < times[j][0] })
+	for _, e := range times {
+		arrival, leaving, i := e[0], e[1], e[2]
+		for len(busy) > 0 && busy[0].t <= arrival {
+			heap.Push(&idle, heap.Pop(&busy).(pair).i)
+		}
+		j := heap.Pop(&idle).(int)
+		if i == targetFriend {
+			return j
+		}
+		heap.Push(&busy, pair{leaving, j})
+	}
+	return -1
+}
+
+type hp struct{ sort.IntSlice }
+
+func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
+func (h *hp) Push(v any)        { h.IntSlice = append(h.IntSlice, v.(int)) }
+func (h *hp) Pop() any {
+	a := h.IntSlice
+	v := a[len(a)-1]
+	h.IntSlice = a[:len(a)-1]
+	return v
+}
+
+type pair struct{ t, i int }
+type hp2 []pair
+
+func (h hp2) Len() int           { return len(h) }
+func (h hp2) Less(i, j int) bool { return h[i].t < h[j].t || (h[i].t == h[j].t && h[i].i < h[j].i) }
+func (h hp2) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }
+func (h *hp2) Push(v any)        { *h = append(*h, v.(pair)) }
+func (h *hp2) Pop() any          { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
+```
+
+#### TypeScript
+
+```ts
+function smallestChair(times: number[][], targetFriend: number): number {
+    const n = times.length;
+    const idle = new MinPriorityQueue();
+    const busy = new MinPriorityQueue({ priority: v => v[0] });
+    for (let i = 0; i < n; ++i) {
+        times[i].push(i);
+        idle.enqueue(i);
+    }
+    times.sort((a, b) => a[0] - b[0]);
+    for (const [arrival, leaving, i] of times) {
+        while (busy.size() > 0 && busy.front().element[0] <= arrival) {
+            idle.enqueue(busy.dequeue().element[1]);
+        }
+        const j = idle.dequeue().element;
+        if (i === targetFriend) {
+            return j;
+        }
+        busy.enqueue([leaving, j]);
+    }
+    return -1;
+}
+```
+
+#### JavaScript
+
+```js
+/**
+ * @param {number[][]} times
+ * @param {number} targetFriend
+ * @return {number}
+ */
+var smallestChair = function (times, targetFriend) {
+    const n = times.length;
+    const idle = new MinPriorityQueue();
+    const busy = new MinPriorityQueue({ priority: v => v[0] });
+    for (let i = 0; i < n; ++i) {
+        times[i].push(i);
+        idle.enqueue(i);
+    }
+    times.sort((a, b) => a[0] - b[0]);
+    for (const [arrival, leaving, i] of times) {
+        while (busy.size() > 0 && busy.front().element[0] <= arrival) {
+            idle.enqueue(busy.dequeue().element[1]);
+        }
+        const j = idle.dequeue().element;
+        if (i === targetFriend) {
+            return j;
+        }
+        busy.enqueue([leaving, j]);
+    }
+};
+```
+
 
 
 
diff --git a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.cpp b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.cpp
index 9d8e503af4952..e3495d13ec7d8 100644
--- a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.cpp	
+++ b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.cpp	
@@ -1,27 +1,28 @@
-using pii = pair;
-
 class Solution {
 public:
     int smallestChair(vector>& times, int targetFriend) {
-        priority_queue, greater> q;
+        using pii = pair;
         priority_queue, greater> busy;
+        priority_queue, greater> idle;
         int n = times.size();
         for (int i = 0; i < n; ++i) {
             times[i].push_back(i);
-            q.push(i);
+            idle.push(i);
         }
-        sort(times.begin(), times.end());
-        for (auto& t : times) {
-            int a = t[0], b = t[1], i = t[2];
-            while (!busy.empty() && busy.top().first <= a) {
-                q.push(busy.top().second);
+        ranges::sort(times);
+        for (const auto& e : times) {
+            int arrival = e[0], leaving = e[1], i = e[2];
+            while (!busy.empty() && busy.top().first <= arrival) {
+                idle.push(busy.top().second);
                 busy.pop();
             }
-            int c = q.top();
-            q.pop();
-            if (i == targetFriend) return c;
-            busy.push({b, c});
+            int j = idle.top();
+            if (i == targetFriend) {
+                return j;
+            }
+            idle.pop();
+            busy.emplace(leaving, j);
         }
         return -1;
     }
-};
\ No newline at end of file
+};
diff --git a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.go b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.go
new file mode 100644
index 0000000000000..80055ced17bea
--- /dev/null
+++ b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.go	
@@ -0,0 +1,41 @@
+func smallestChair(times [][]int, targetFriend int) int {
+	idle := hp{}
+	busy := hp2{}
+	for i := range times {
+		times[i] = append(times[i], i)
+		heap.Push(&idle, i)
+	}
+	sort.Slice(times, func(i, j int) bool { return times[i][0] < times[j][0] })
+	for _, e := range times {
+		arrival, leaving, i := e[0], e[1], e[2]
+		for len(busy) > 0 && busy[0].t <= arrival {
+			heap.Push(&idle, heap.Pop(&busy).(pair).i)
+		}
+		j := heap.Pop(&idle).(int)
+		if i == targetFriend {
+			return j
+		}
+		heap.Push(&busy, pair{leaving, j})
+	}
+	return -1
+}
+
+type hp struct{ sort.IntSlice }
+
+func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
+func (h *hp) Push(v any)        { h.IntSlice = append(h.IntSlice, v.(int)) }
+func (h *hp) Pop() any {
+	a := h.IntSlice
+	v := a[len(a)-1]
+	h.IntSlice = a[:len(a)-1]
+	return v
+}
+
+type pair struct{ t, i int }
+type hp2 []pair
+
+func (h hp2) Len() int           { return len(h) }
+func (h hp2) Less(i, j int) bool { return h[i].t < h[j].t || (h[i].t == h[j].t && h[i].i < h[j].i) }
+func (h hp2) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }
+func (h *hp2) Push(v any)        { *h = append(*h, v.(pair)) }
+func (h *hp2) Pop() any          { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
diff --git a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.java b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.java
index b7731d63b0b4d..9dfa455c1d7f6 100644
--- a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.java	
+++ b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.java	
@@ -1,25 +1,24 @@
 class Solution {
     public int smallestChair(int[][] times, int targetFriend) {
         int n = times.length;
-        int[][] ts = new int[n][3];
-        PriorityQueue q = new PriorityQueue<>();
-        PriorityQueue busy = new PriorityQueue<>((a, b) -> a[0] - b[0]);
+        PriorityQueue idle = new PriorityQueue<>();
+        PriorityQueue busy = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
         for (int i = 0; i < n; ++i) {
-            ts[i] = new int[] {times[i][0], times[i][1], i};
-            q.offer(i);
+            times[i] = new int[] {times[i][0], times[i][1], i};
+            idle.offer(i);
         }
-        Arrays.sort(ts, (a, b) -> a[0] - b[0]);
-        for (int[] t : ts) {
-            int a = t[0], b = t[1], i = t[2];
-            while (!busy.isEmpty() && busy.peek()[0] <= a) {
-                q.offer(busy.poll()[1]);
+        Arrays.sort(times, Comparator.comparingInt(a -> a[0]));
+        for (var e : times) {
+            int arrival = e[0], leaving = e[1], i = e[2];
+            while (!busy.isEmpty() && busy.peek()[0] <= arrival) {
+                idle.offer(busy.poll()[1]);
             }
-            int c = q.poll();
+            int j = idle.poll();
             if (i == targetFriend) {
-                return c;
+                return j;
             }
-            busy.offer(new int[] {b, c});
+            busy.offer(new int[] {leaving, j});
         }
         return -1;
     }
-}
\ No newline at end of file
+}
diff --git a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.js b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.js
new file mode 100644
index 0000000000000..d09b5aa4d9c33
--- /dev/null
+++ b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.js	
@@ -0,0 +1,25 @@
+/**
+ * @param {number[][]} times
+ * @param {number} targetFriend
+ * @return {number}
+ */
+var smallestChair = function (times, targetFriend) {
+    const n = times.length;
+    const idle = new MinPriorityQueue();
+    const busy = new MinPriorityQueue({ priority: v => v[0] });
+    for (let i = 0; i < n; ++i) {
+        times[i].push(i);
+        idle.enqueue(i);
+    }
+    times.sort((a, b) => a[0] - b[0]);
+    for (const [arrival, leaving, i] of times) {
+        while (busy.size() > 0 && busy.front().element[0] <= arrival) {
+            idle.enqueue(busy.dequeue().element[1]);
+        }
+        const j = idle.dequeue().element;
+        if (i === targetFriend) {
+            return j;
+        }
+        busy.enqueue([leaving, j]);
+    }
+};
diff --git a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.py b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.py
index ae380be2ff2c1..5cfb495c1886c 100644
--- a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.py	
+++ b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.py	
@@ -1,17 +1,16 @@
 class Solution:
     def smallestChair(self, times: List[List[int]], targetFriend: int) -> int:
         n = len(times)
-        h = list(range(n))
-        heapify(h)
         for i in range(n):
             times[i].append(i)
         times.sort()
+        idle = list(range(n))
+        heapify(idle)
         busy = []
-        for a, b, i in times:
-            while busy and busy[0][0] <= a:
-                heappush(h, heappop(busy)[1])
-            c = heappop(h)
+        for arrival, leaving, i in times:
+            while busy and busy[0][0] <= arrival:
+                heappush(idle, heappop(busy)[1])
+            j = heappop(idle)
             if i == targetFriend:
-                return c
-            heappush(busy, (b, c))
-        return -1
+                return j
+            heappush(busy, (leaving, j))
diff --git a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.ts b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.ts
new file mode 100644
index 0000000000000..9f84fb55b0950
--- /dev/null
+++ b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.ts	
@@ -0,0 +1,21 @@
+function smallestChair(times: number[][], targetFriend: number): number {
+    const n = times.length;
+    const idle = new MinPriorityQueue();
+    const busy = new MinPriorityQueue({ priority: v => v[0] });
+    for (let i = 0; i < n; ++i) {
+        times[i].push(i);
+        idle.enqueue(i);
+    }
+    times.sort((a, b) => a[0] - b[0]);
+    for (const [arrival, leaving, i] of times) {
+        while (busy.size() > 0 && busy.front().element[0] <= arrival) {
+            idle.enqueue(busy.dequeue().element[1]);
+        }
+        const j = idle.dequeue().element;
+        if (i === targetFriend) {
+            return j;
+        }
+        busy.enqueue([leaving, j]);
+    }
+    return -1;
+}