Advertisement

Seat Reservation Manager - LeetCode 1845 Solution

Seat Reservation Manager - Complete Solution Guide

Seat Reservation Manager is LeetCode problem 1845, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 . Example 1: Input ["SeatManager", "reserve", "reserve", "unres

Detailed Explanation

The problem asks us to design a system, the `SeatManager`, which manages the reservation status of `n` seats, numbered from 1 to `n`. Initially, all seats are available. The `SeatManager` needs to implement two core functionalities: `reserve()` and `unreserve(seatNumber)`. The `reserve()` function should return the smallest-numbered available seat, reserving it in the process. The `unreserve(seatNumber)` function should make the specified seat available again.

Solution Approach

The provided code uses a min-heap (priority queue) to store the available seats. When a seat is reserved, if there are available seats in the heap, the smallest seat number is extracted from the heap and returned. Otherwise, the next available seat number is used, and the `next_seat` counter is incremented. When a seat is unreserved, its number is added to the heap. The min-heap property ensures that the `reserve()` function always returns the smallest available seat.

Step-by-Step Algorithm

  1. Step 1: Initialize the `SeatManager` with `n` (number of seats), `next_seat` (starting at 1), and an empty min-heap `available_seats`.
  2. Step 2: In the `reserve()` function, check if the `available_seats` heap is empty.
  3. Step 3: If the heap is not empty, extract the minimum element (smallest available seat) from the heap and return it. This seat is now reserved.
  4. Step 4: If the heap is empty, return the value of `next_seat` and increment `next_seat`. This allocates the next available seat number.
  5. Step 5: In the `unreserve(seatNumber)` function, add the `seatNumber` back into the `available_seats` min-heap. This makes the seat available for reservation again.

Key Insights

  • Insight 1: The smallest-numbered available seat is crucial, suggesting the use of a data structure that can efficiently find the minimum element.
  • Insight 2: A min-heap (priority queue) is a suitable data structure to maintain the set of available seats, as it provides O(log n) time complexity for insertion (unreserve) and extracting the minimum element (reserve).
  • Insight 3: It is important to track the next available seat number to use when the heap is empty. This ensures that seat numbers are assigned in increasing order when all previously unreserved seats have been reserved.

Complexity Analysis

Time Complexity: O(log n)

Space Complexity: O(n)

Topics

This problem involves: Design, Heap (Priority Queue).

Companies

Asked at: Dropbox, Siemens.