Practice/Meta/Leetcode 1845. Seat Reservation Manager
CodingOptional
Design a system to manage seat reservations for a theater with n seats numbered from 1 to n. Initially, all seats are available for booking.
Your system must support two operations:
The system must efficiently handle up to 100,000 operations in total, so your implementation should prioritize performance for both operations.
SeatManager class that takes n (total number of seats) as a constructor parameterreserve() method should return the smallest available seat number and mark it as reservedunreserve(seatNumber) method should make the specified seat available againreserve() is only called when at least one seat is availableunreserve() is only called for seats that are currently reservedreserve() and unreserve()reserve() is only called when there is at least one unreserved seatunreserve() is only called with a seat that is currently reservedExample 1:
SeatManager seatManager = new SeatManager(5); seatManager.reserve(); // Returns 1 (smallest available) seatManager.reserve(); // Returns 2 (smallest available) seatManager.unreserve(2); // Seat 2 becomes available seatManager.reserve(); // Returns 2 (smallest available) seatManager.reserve(); // Returns 3 (smallest available) seatManager.reserve(); // Returns 4 (smallest available) seatManager.unreserve(5); // Does nothing since seat 5 was never reserved
Example 2:
SeatManager seatManager = new SeatManager(3); seatManager.reserve(); // Returns 1 seatManager.reserve(); // Returns 2 seatManager.reserve(); // Returns 3 seatManager.unreserve(1); // Seat 1 becomes available seatManager.unreserve(3); // Seat 3 becomes available seatManager.reserve(); // Returns 1 (smallest available, not 3)