Imagine you're designing a system for a university exam room with n seats arranged in a single row, numbered from 0 to n-1. Students want to maintain maximum social distance while taking their exam!
When a student enters the room, they must sit in the seat that maximizes the distance to the closest occupied seat. If there are multiple seats with the same maximum distance, they choose the seat with the lowest number. If the room is empty, they sit at seat 0.
Your task is to implement an ExamRoom class that simulates this behavior:
ExamRoom(int n): Initialize the exam room withnseatsint seat(): Return the seat number where the next student sitsvoid leave(int p): Remove the student from seatp
Goal: Efficiently manage seat assignments to maximize distances between students.
Input & Output
Visualization
Time & Space Complexity
For each seat() operation, we scan through occupied seats once to find largest gap
Space for storing occupied seats in sorted order
Constraints
- 1 ≤ n ≤ 109
- At most 104 calls will be made to seat and leave
- seat() is guaranteed to return a valid seat number
- leave(p) is guaranteed to be called with an occupied seat p