Description
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§ 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§ are guaranteed to have a student currently sitting in seat number p.
Solution
构建一个Exam room类,传入一行的座位数N,每次通过调用seat()的方法可以安排一个考生坐下,要保证考生之间的距离均匀且最大,如果距离相同则尽量靠左安排,也就是说第一个考生坐在0,第二个坐在N - 1。
leave()方法保证传入一个有人坐的座位,将它删除。
We construct an interval class, which denotes the free space between two student. x is left one, y is right one, dist is the mid distance between two student, that is the new seat position. There are two special case, if x == -1, dist = y; y == N, dist = N - 1 - x. Because we add two boundary on both side, so it means left or right is empty and ready for next seat.
In constructor, initialize a priority queue which poll max dist first and if dist is equal, poll left seat first. push an initial boundary interval [-1, N] in it.
In seat(), we poll out interval, if interval.x = -1, seat there, elif y = N, seat there; Or we should calculate the mid position and push two new interavals in pq.
In leave, convert pq to a list then iteratively find two intervals. One starts at p and one end at p. Remove these two intervals, and then push a new interval which covers them in pq.
Code
class ExamRoom {
private PriorityQueue<Interval> pq;
private int N;
class Interval{
int x;
int y;
int dist;
public Interval(int x, int y){
this.x = x;
this.y = y;
if (x == -1){
this.dist = y;
}
else if (y == N){
this.dist = N - 1 - x;
}
else{
this.dist = Math.abs(y - x) / 2;
}
}
}
public ExamRoom(int N) {
this.pq = new PriorityQueue<Interval>((a, b) -> a.dist != b.dist ? b.dist - a.dist : a.x - b.x);
this.N = N;
pq.add(new Interval(-1, N));
}
public int seat() {
int seat = 0;
Interval in = pq.poll();
if (in.x == -1){
seat = 0;
}
else if(in.y == N){
seat = N - 1;
}
else{
seat = (in.x + in.y) / 2;
}
pq.offer(new Interval(in.x, seat));
pq.offer(new Interval(seat, in.y));
return seat;
}
public void leave(int p) {
Interval head = null, tail = null;
List<Interval> list = new ArrayList<>(pq);
for (Interval in : list){
if (in.x == p){
tail = in;
}
if (in.y == p){
head = in;
}
if (head != null && tail != null){
break;
}
}
pq.remove(head);
pq.remove(tail);
pq.offer(new Interval(head.x, tail.y));
}
}
/**
* Your ExamRoom object will be instantiated and called as such:
* ExamRoom obj = new ExamRoom(N);
* int param_1 = obj.seat();
* obj.leave(p);
*/
Time Complexity: O(nlogN)
Space Complexity: O(N)