You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/1800-1899/1845.Seat Reservation Manager/README_EN.md
+39-16
Original file line number
Diff line number
Diff line change
@@ -68,17 +68,15 @@ seatManager.unreserve(5); // Unreserve seat 5, so now the available seats are [5
68
68
69
69
<!-- solution:start -->
70
70
71
-
### Solution 1: Priority Queue (MinHeap)
71
+
### Solution 1: Priority Queue (Min-Heap)
72
72
73
-
We can use a priority queue (minheap) to maintain the smallest number of reservable seats.
73
+
We define a priority queue (min-heap) $\textit{q}$ to store all the available seat numbers. Initially, we add all seat numbers from $1$ to $n$ into $\textit{q}$.
74
74
75
-
Initially, put all seat numbers into the priority queue.
75
+
When calling the `reserve` method, we pop the top element from $\textit{q}$, which is the smallest available seat number.
76
76
77
-
When the `reserve` method is called, take out the smallest number from the priority queue, which is the smallest number of reservable seats.
77
+
When calling the `unreserve` method, we add the seat number back into $\textit{q}$.
78
78
79
-
When the `unreserve` method is called, put the seat number back into the priority queue.
80
-
81
-
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the number of seats.
79
+
In terms of time complexity, the initialization time complexity is $O(n)$ or $O(n \times \log n)$, and the time complexity of the `reserve` and `unreserve` methods is both $O(\log n)$. The space complexity is $O(n)$.
82
80
83
81
<!-- tabs:start -->
84
82
@@ -88,7 +86,6 @@ The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$.
88
86
classSeatManager:
89
87
def__init__(self, n: int):
90
88
self.q =list(range(1, n +1))
91
-
heapify(self.q)
92
89
93
90
defreserve(self) -> int:
94
91
return heappop(self.q)
@@ -207,27 +204,53 @@ func (h *hp) Pop() any {
207
204
*/
208
205
```
209
206
207
+
#### TypeScript
208
+
209
+
```ts
210
+
classSeatManager {
211
+
private q:typeofMinPriorityQueue;
212
+
constructor(n:number) {
213
+
this.q=newMinPriorityQueue();
214
+
for (let i =1; i<=n; i++) {
215
+
this.q.enqueue(i);
216
+
}
217
+
}
218
+
219
+
reserve():number {
220
+
returnthis.q.dequeue().element;
221
+
}
222
+
223
+
unreserve(seatNumber:number):void {
224
+
this.q.enqueue(seatNumber);
225
+
}
226
+
}
227
+
228
+
/**
229
+
* Your SeatManager object will be instantiated and called as such:
0 commit comments