Practice/Apple/Leetcode 855. Exam Room
CodingMust
Design a seating system for a concert hall with n seats arranged in a single row, numbered from 0 to n-1.
When someone arrives and calls seat(), they should be assigned to the seat that maximizes the distance to the closest already-occupied seat. If there are multiple such seats, choose the one with the smallest index.
When someone leaves and calls leave(p), seat p becomes available again.
Your task is to implement the ConcertHall class with these methods:
ConcertHall(n): Initializes a concert hall with n seats, all initially emptyseat(): Returns the seat number where the next person should sitleave(p): Marks seat p as emptyseat() and leave()leave(p) will only be called on an occupied seatExample 1:
ConcertHall hall = ConcertHall(10); hall.seat(); // Returns 0 (first person sits at leftmost seat) hall.seat(); // Returns 9 (maximize distance from 0) hall.seat(); // Returns 4 (middle of gap between 0 and 9) hall.leave(4); // Seat 4 becomes empty hall.seat(); // Returns 4 (again the optimal position)
Example 2:
ConcertHall hall = ConcertHall(8); hall.seat(); // Returns 0 hall.seat(); // Returns 7 hall.seat(); // Returns 3 (middle of 0-7, distance 3 to nearest) hall.seat(); // Returns 5 (middle of 3-7, distance 2; also middle of 0-3 is 1 with distance 1) hall.leave(3); // Seat 3 becomes empty hall.seat(); // Returns 3 (recreates the gap)