In an exam room, there areĀ NĀ seats in a single row, numberedĀ 0, 1, 2, ..., 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.Ā (Also, if no one is in the room, then the student sits at seat number 0.)
Return a classĀ ExamRoom(int N)Ā that exposes two functions:Ā ExamRoom.seat()Ā returning anĀ intĀ representing what seat the student sat in, andĀ ExamRoom.leave(int p)Ā representing that the student in seat numberĀ pĀ now leaves the room.Ā It is guaranteed that any calls toĀ ExamRoom.leave(p)Ā have a student sitting in seatĀ p.
Example 1:
Input: ["ExamRoom","seat","seat","seat","seat","leave","seat"], [[10],[],[],[],[],[4],[]]Output: [null,0,9,4,2,null,5]Explanation:
ExamRoom(10) -> null
seat() -> 0, no one is in the room, then the student sits at seat number 0.
seat() -> 9, the student sits at the last seat number 9.
seat() -> 4, the student sits at the last seat number 4.
seat() -> 2, the student sits at the last seat number 2.
leave(4) -> null
seat() -> 5, the studentāāāāāāā sits at the last seat number 5.
āāāāNote:
1 <= N <= 10^9
ExamRoom.seat()Ā andĀ ExamRoom.leave()Ā will be called at mostĀ 10^4Ā times across all test cases.
Calls toĀ ExamRoom.leave(p)Ā are guaranteed to have a student currently sitting in seat numberĀ p.
Solution: BST
Use a BST (ordered set) to track the current seatings.