Exam Room - Complete Solution Guide
Exam Room is LeetCode problem 855, 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
There is an exam room with n seats in a single row labeled from 0 to n - 1 . When a student enters the room, they must sit in the seat that maximizes the distance to the closest person. If there are multiple such seats, they sit in the seat with the lowest number. If no one is in the room, then the student sits at seat number 0 . Design a class that simulates the mentioned exam room. Implement the ExamRoom class: ExamRoom(int n) Initializes the object of the exam room with the number of the seat
Detailed Explanation
The problem asks us to simulate an exam room seating arrangement. The exam room has 'n' seats numbered from 0 to n-1. Students enter the room one at a time and must sit in the seat that maximizes their distance to the closest seated student. If there are multiple such seats, the student chooses the seat with the smallest number. If the room is empty, the student sits in seat 0. We need to implement an `ExamRoom` class with the following methods: `ExamRoom(int n)` to initialize the room with 'n' seats, `seat()` to return the seat number for the next student, and `leave(int p)` to indicate that the student in seat 'p' is leaving.
Solution Approach
The solution uses a sorted list called `students` to store the positions of the seated students. When a student wants to sit, the algorithm iterates through the list, calculates the distance between adjacent students, and finds the maximum distance to a seated student. Special care is taken to handle the edges (seat 0 and seat n-1). The `bisect.insort` method in Python or equivalent methods in Java and C++ are used to maintain the sorted order after a student takes a seat. When a student leaves, the algorithm removes their seat number from the `students` list.
Step-by-Step Algorithm
- Step 1: Initialize the ExamRoom with 'n' total seats and an empty list called `students` to track occupied seats.
- Step 2: In the `seat()` method, if `students` is empty, return seat 0 and add 0 to the `students` list.
- Step 3: If `students` is not empty, calculate the distance from seat 0 to the first student (`students[0]`).
- Step 4: Iterate through the `students` list, calculating the distance between each pair of adjacent students (`students[i+1] - students[i]`) and divide by 2 to find the midpoint distance.
- Step 5: Calculate the distance from the last student to the end of the room (`n - 1 - students[-1]`).
- Step 6: Compare all three distances (start, middle, end) to find the maximum distance. The corresponding seat (either 0, the midpoint, or n-1) is the best seat.
- Step 7: Insert the best seat into the `students` list in sorted order using `bisect.insort` (Python) or equivalent methods in Java/C++.
- Step 8: In the `leave(p)` method, remove the seat number `p` from the `students` list.
Key Insights
- Insight 1: Maintaining a sorted list of occupied seats allows efficient calculation of distances to neighboring students.
- Insight 2: The maximum distance can occur at the beginning of the row (seat 0), in the middle between two students, or at the end of the row (seat n-1).
- Insight 3: Binary search (or similar sorted insertion) is crucial for efficiently placing the student in the correct sorted position within the 'students' list.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Design, Heap (Priority Queue), Ordered Set.
Companies
Asked at: Quora.