Skip to main content

Exam Room

MediumArrayGreedy

Description

Design an exam room where students sit to maximize distance from closest person. Implement seat() and leave(). Return the result as an array.

Examples

Input:n=10, ["seat","seat","seat","seat"]
Output:[0,9,4,2]
Explanation:

First student takes seat 0. Second takes seat 9 (far end). Third takes seat 4 (middle). Fourth takes seat 2.

Input:n=5, ["seat","seat","leave",0,"seat"]
Output:[0,4,null,2]
Explanation:

First seat goes to position 0. Second seat goes to position 4 (maximum distance). Leave position 0. Next seat goes to position 2 (middle of now-empty range 0-4, maximizing distance from person at 4).

Input:n=3, ["seat","seat","seat"]
Output:[0,2,1]
Explanation:

In a small room with only 3 seats: first person sits at position 0, second at position 2 (maximum distance), third must sit at position 1 (the only remaining seat between them).

Constraints

  • 1 ≤ n ≤ 10⁹

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.