# 855 Exam Room

## 855. [Exam Room](https://leetcode.com/problems/exam-room/description/)

## 1. Question

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. `1 <= N <= 10^9`
2. `ExamRoom.seat()`and`ExamRoom.leave()`will be called at most`10^4`times across all test cases.
3. Calls to`ExamRoom.leave(p)`are guaranteed to have a student currently sitting in seat number`p`.

## 2. Implementation

**(1) Heap**

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

```java
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:**&#x65F6;间复杂度seat(): O(logN), leave(): O(N), 空间复杂度O(N)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://protegejj.gitbook.io/oj-practices/chapter1/heap/855-exam-room.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
