Problem

Design a system that manages the reservation state of n seats that are numbered from 1 to n.

Implement the SeatManager class:

  • SeatManager(int n) Initializes a SeatManager object that will manage n seats numbered from 1 to n. All seats are initially available.
  • int reserve() Fetches the smallest-numbered unreserved seat, reserves it, and returns its number.
  • void unreserve(int seatNumber) Unreserves the seat with the given seatNumber.

Examples

Example 1:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
**Input**
["SeatManager", "reserve", "reserve", "unreserve", "reserve", "reserve", "reserve", "reserve", "unreserve"]
[[5], [], [], [2], [], [], [], [], [5]]
**Output**
[null, 1, 2, null, 2, 3, 4, 5, null]

**Explanation**
SeatManager seatManager = new SeatManager(5); // Initializes a SeatManager with 5 seats.
seatManager.reserve();    // All seats are available, so return the lowest numbered seat, which is 1.
seatManager.reserve();    // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
seatManager.unreserve(2); // Unreserve seat 2, so now the available seats are [2,3,4,5].
seatManager.reserve();    // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
seatManager.reserve();    // The available seats are [3,4,5], so return the lowest of them, which is 3.
seatManager.reserve();    // The available seats are [4,5], so return the lowest of them, which is 4.
seatManager.reserve();    // The only available seat is seat 5, so return 5.
seatManager.unreserve(5); // Unreserve seat 5, so now the available seats are [5].

Solution

Method 1 – MinHeap with Lazy Initialization

Intuition

To efficiently manage seat reservations, we use a min-heap to keep track of seats that have been unreserved. Instead of initializing the heap with all seat numbers, we maintain a counter for the next available seat. This way, we only add seats to the heap when they are unreserved, saving both time and space.

Approach

  1. Use a min-heap to store seat numbers that have been unreserved.
  2. Maintain a counter to track the next seat to reserve.
  3. On reserve, return the smallest seat number from the heap if available; otherwise, use the counter and increment it.
  4. On unreserve, add the seat number back to the heap.

Complexity

  • ⏰ Time complexity: O(log n) for reserve and unreserve due to heap operations.
  • 🧺 Space complexity: O(n), for storing unreserved seat numbers in the heap.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <queue>
class SeatManager {
    priority_queue<int, vector<int>, greater<int>> pq;
    int next;
public:
    SeatManager(int n) : next(1) {}
    int reserve() {
        if (!pq.empty()) {
            int seat = pq.top(); pq.pop();
            return seat;
        }
        return next++;
    }
    void unreserve(int seatNumber) {
        pq.push(seatNumber);
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import java.util.PriorityQueue;
class SeatManager {
    private final PriorityQueue<Integer> pq = new PriorityQueue<>();
    private int next = 1;
    public SeatManager(int n) {}
    public int reserve() {
        if (!pq.isEmpty()) {
            return pq.poll();
        }
        return next++;
    }
    public void unreserve(int seatNumber) {
        pq.offer(seatNumber);
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import heapq
class SeatManager:
    def __init__(self, n: int) -> None:
        self.pq: list[int] = []
        self.next: int = 1
    def reserve(self) -> int:
        if self.pq:
            return heapq.heappop(self.pq)
        ans = self.next
        self.next += 1
        return ans
    def unreserve(self, seatNumber: int) -> None:
        heapq.heappush(self.pq, seatNumber)