855 Exam Room

1. Question

In an exam room, there areNseats in a single row, numbered0, 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 classExamRoom(int N) that exposes two functions:ExamRoom.seat() returning anint representing what seat the student sat in, andExamRoom.leave(int p) representing that the student in seat numberp now leaves the room. It is guaranteed that any calls toExamRoom.leave(p)have a student sitting in seatp.

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. 1 <= N <= 10^9

  2. ExamRoom.seat()andExamRoom.leave()will be called at most10^4times across all test cases.

  3. Calls toExamRoom.leave(p)are guaranteed to have a student currently sitting in seat numberp.

2. Implementation

(1) Heap

思路: 用maxHeap保留区间,按照区间距离从大到小排序。call Leave()时,则需要遍历heap找出包含p点的左区间和右区间,将这两个区间删除,并将左区间的最左点和右区间的最右点合并成一个新区间

class ExamRoom {
    int N;
    PriorityQueue<int[]> maxHeap;

    public int intervalToDist(int[] interval) {
        int dist = 0;
        if (interval[0] == -1) {
            dist = interval[1];
        }
        else if (interval[1] == N) {
            dist = N - 1 - interval[0];
        }
        else {
            dist = (interval[1] - interval[0]) / 2;
        }
        return dist;
    }

    public ExamRoom(int N) {
        this.N = N;
        maxHeap = new PriorityQueue<int[]>((a, b)-> {
            int distA = intervalToDist(a);
            int distB = intervalToDist(b);

            if (distA == distB) {
                return a[0] - b[0];
            }
            else {
                return distB - distA;
            }
        });
        maxHeap.add(new int[] {-1, N});
    }

    public int seat() {
        int res = 0;
        int[] interval = maxHeap.remove();

        if (interval[0] == -1) {
            res = 0;
        } 
        else if (interval[1] == N) {
            res = N - 1;
        }
        else {
            res = interval[0] + (interval[1] - interval[0]) / 2;
        }

        maxHeap.add(new int[] {interval[0], res});
        maxHeap.add(new int[] {res, interval[1]});
        return res;
    }

    public void leave(int p) {
        int[] leftInterval = null;
        int[] rightInterval = null;

        for (int[] interval : maxHeap) {
            if (interval[1] == p) {
                leftInterval = interval;
            }
            if (interval[0] == p) {
                rightInterval = interval;
            }
        }

        maxHeap.remove(leftInterval);
        maxHeap.remove(rightInterval);
        maxHeap.add(new int[] {leftInterval[0], rightInterval[1]});
    }
}

/**
 * Your ExamRoom object will be instantiated and called as such:
 * ExamRoom obj = new ExamRoom(N);
 * int param_1 = obj.seat();
 * obj.leave(p);
 */

3. Time & Space Complexity

Heap:时间复杂度seat(): O(logN), leave(): O(N), 空间复杂度O(N)

Last updated

Was this helpful?