Seat Reservation Manager
MediumHeap / Priority Queue
Decode it first. Clues, constraints, pattern and plan before any code.A theatre has n seats numbered 1 to n, all free at the start. Build a class SeatManager:
SeatManager(n)sets up thenseats.reserve()books the smallest-numbered free seat and returns its number.unreserve(seat_number)(seatNumberin TypeScript) frees that seat again.
m = SeatManager(5)
m.reserve() → 1
m.reserve() → 2
m.unreserve(2)
m.reserve() → 2
m.reserve() → 3Constraints
1 <= n <= 10^5- At most
10^5calls are made in total. reserveis only called when at least one seat is free.unreserveis only called on a seat that is currently reserved.