2Brute2Best
🔥0

Seat Reservation Manager

Medium·LeetCode #1845 Pro OnlySlight Variation
LeetCode
01

Problem Statement

A hall has n seats numbered from 1 to n. Build a class SeatManager that hands out seats.

  • SeatManager(int n) starts with all n seats free.
  • int reserve() takes the smallest-numbered free seat, marks it taken, and returns its number.
  • void unreserve(int seatNumber) makes a taken seat free again.
Input

The number n, then a series of reserve() and unreserve(seatNumber) calls.

Output

For each reserve() call, the seat number it gave out. unreserve returns nothing.

Example 1
Input: n = 5; reserve(), reserve(), unreserve(2), reserve(), reserve(), reserve(), reserve(), unreserve(5)
Output: 1, 2, 2, 3, 4, 5

Seats 1 and 2 go out first. Seat 2 is given back, so the next reserve takes 2 again, because it is the smallest free seat. Then 3, 4 and 5 go out in order. Giving back 5 leaves it free.

Example 2
Input: n = 3; reserve(), reserve(), reserve(), unreserve(3), unreserve(1), reserve()
Output: 1, 2, 3, 1

After unreserve(3) and unreserve(1), seats 1 and 3 are free. The smallest one, 1, comes out first, even though seat 3 was given back earlier.

Constraints
  • • 1 ≤ n ≤ 10^5
  • • 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 that seatNumber will be reserved
  • • At most 10^5 calls in total will be made to reserve and unreserve
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“smallest-numbered free seat”
→ We need the minimum of a set that keeps changing → a min-heap of free seats, not a scan.
“unreserve can give back any taken seat”
→ A returned seat can be smaller than seats still in use, so it has to be stored for reuse.
“n up to 10^5”
→ Don't build the whole structure up front. A counter covers the never-used block for free.
03

Approach Complexity Ladder

Brute Force — one flag per seat, scan from 1O(n)O(n)

Boolean array of taken seats; reserve scans from seat 1 for the first free one

Better — min-heap of every free seatO(log n)O(n)

Put all n seats in a min-heap at the start; reserve pops the top, unreserve pushes back

Optimal — counter + min-heap of given-back seatsO(log m)O(m)

A counter covers the never-used seats; a min-heap holds only the seats that were given back

Brute Force Intuition

Keep taken[s] for every seat. reserve scans from seat 1 and takes the first seat whose flag is false. unreserve clears that seat's flag.

Recognition rule for this sub-pattern:

Items keep arriving; maintain the heap so each query is O(log K) or O(1).
Remember this · Heap — Top K
Trigger
"K largest / smallest / most frequent / closest", kth element, streaming top-K.
Think
Do I really need everything sorted, or only the best K?
Key question
Min-heap or max-heap of size K — which one lets me kick out the worst candidate?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Heap — Top K translate into production architectures and low-level memory mechanics.