
Problem Statement
In this problem, we are tasked with designing a system, specifically a SeatManager
class that manages seat reservations. This system should be capable of overseeing a series of seats numbered sequentially from 1
to n
, with each seat initially unreserved. The SeatManager
class will provide functionalities to both reserve and unreserve these seats:
SeatManager(int n)
: Constructor method which initializes aSeatManager
object managingn
seats, all initially unreserved.int reserve()
: A function that reserves the smallest-numbered seat that is currently unreserved, then returns the seat number to confirm the reservation.void unreserve(int seatNumber)
: A method used to unreserve a seat that has previously been reserved, identified by its seat numberseatNumber
.
Examples
Example 1
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].
Constraints
1 <= n <= 10⁵
1 <= seatNumber <= n
- For each call to
reserve
, it is guaranteed that there will be at least one unreserved seat. - For each call to
unreserve
, it is guaranteed thatseatNumber
will be reserved. - At most
10⁵
calls in total will be made toreserve
andunreserve
.
Approach and Intuition
The operation of the SeatManager
class revolves around efficiently managing the availability of the seats. Below is a detailed explanation and intuitive understanding using the system of the example provided:
Initialization (
SeatManager
Construction):- A
SeatManager
is created with a predefined number of seats (for instance, 5 seats in the example). These seats are all marked as unreserved initially.
- A
Reserving a Seat (
reserve
method):- When the
reserve
method is called, it is required to return the smallest numbered seat that has not been reserved yet. - After a seat is reserved, it should be marked so that it is not available for subsequent
reserve
calls unless it has been unreserved.
- When the
Unreserving a Seat (
unreserve
method):- The
unreserve
function permits a previously reserved seat to be made available again. - The method receives a specific seat number and makes that seat available for future
reserve
calls, without affecting the reservation state of other seats.
- The
This method of seat reservation and unreservation needs to efficiently track the lowest available seat. The constraints given ensure that the operations are feasible within the system's limits, with guarantees of availability and correctness for every reserve
and unreserve
call.
Solutions
- C++
class SeatController {
int current;
set<int> freeSeats;
public:
SeatController(int n) {
current = 1;
}
int reserveSeat() {
if (!freeSeats.empty()) {
int lowestSeat = *freeSeats.begin();
freeSeats.erase(freeSeats.begin());
return lowestSeat;
}
int seat = current;
current++;
return seat;
}
void returnSeat(int seatNumber) {
freeSeats.insert(seatNumber);
}
};
The solution implements a class named SeatController
to manage seat reservations efficiently, using C++ as the programming language. This class utilizes an integer current
to track the next available seat and a set<int>
to manage seats that become available when they are returned. Here’s a breakdown of how the SeatController
operates:
Initialization:
SeatController(int n)
: Initializes thecurrent
seat to 1, effectively setting the first available seat when an instance is created.
Reserve a Seat:
int reserveSeat()
: This method checks if there are any seats in thefreeSeats
set. If available, it reserves the lowest numbered seat, removes it from the set, and returns this seat number. If no seats are available in the set, it reserves the seat number held bycurrent
, incrementscurrent
, and returns the seat number.
Return a Seat:
void returnSeat(int seatNumber)
: When a seat is no longer needed, this method is used to return a seat by inserting theseatNumber
back into thefreeSeats
set. This makes the seat available again for future reservations.
This approach ensures efficient management of seat reservations and returns, allowing for quick allocation of the lowest available seat numbers and handling of dynamically freed seats.
- Java
class SeatAllocation {
int current;
TreeSet<Integer> freeSeats;
public SeatAllocation(int n) {
current = 1;
freeSeats = new TreeSet<>();
}
public int allocate() {
if (!freeSeats.isEmpty()) {
int seat = freeSeats.first();
freeSeats.remove(seat);
return seat;
}
int seat = current;
current++;
return seat;
}
public void release(int seatNumber) {
freeSeats.add(seatNumber);
}
}
This Java solution handles seat reservation management using an efficient approach. The SeatAllocation
class utilizes a TreeSet
to keep track of free seats which ensures that the seats are always sorted, allowing for quick allocations and releases.
Initialize the
SeatAllocation
class with the total number of seats. This sets thecurrent
seat to 1 and prepares a newTreeSet
,freeSeats
, to store available seats that have been released.The
allocate
method checks if there are any seats infreeSeats
. If available, it retrieves and removes the smallest seat number fromfreeSeats
, which represents the lowest available seat number. If no seats are free, it assigns the seat number stored incurrent
, then incrementscurrent
for the next call.The
release
method is straightforward and simply adds the released seat number back to thefreeSeats
set, making it available for future allocations.
This setup ensures that the system efficiently manages seat allocations and keeps track of available seats, optimizing both operations based on the scenario.
- Python
from sortedcontainers import SortedSet
class ReservationManager:
def __init__(self, size):
# Initialize smallest unreserved seat index
self.current_seat = 1
# Store for all available seats
self.unreserved_seats = SortedSet()
def book_seat(self):
# Check and remove the smallest available seat
if self.unreserved_seats:
smallest_seat = self.unreserved_seats.pop(0)
return smallest_seat
# Book by current index and update
smallest_seat = self.current_seat
self.current_seat += 1
return smallest_seat
def free_seat(self, seat_id):
# Return seat to the unreserved set
self.unreserved_seats.add(seat_id)
The solution implements a ReservationManager
class to manage seat reservations. Here's an outline of how the defined class operates using Python and the SortedSet from the sortedcontainers
module:
Initialization of the Reservation Manager:
__init__
: Initializes with a given size for the number of seats. It setscurrent_seat
to 1, indicating the first seat to be reserved sequentially without previous reservations. It also initializesunreserved_seats
as aSortedSet
to keep track of seats that have been freed up and are available for reservation again.
Booking a Seat:
book_seat
: This method checks if there are any unreserved seats available inunreserved_seats
. If available, it removes and returns the smallest number seat from the set, ensuring that the lowest numbered seat is always given out first. If no seats are unreserved, it books the currentcurrent_seat
and then increments it to prepare for the next booking.
Freeing a Seat:
free_seat
: This method takes aseat_id
as an argument and adds it back into theunreserved_seats
set. This action makes the seat available again for future bookings.
The implementation guarantees that seating allocation is efficient, with minimal seat numbers allocated first, and seats that become available again are promptly reused. The use of SortedSet
allows efficient insertion and removal of seats while maintaining order.
No comments yet.