There is an exam room with n seats in a single row labeled from 0 to 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. If no one is in the room, then the student sits at seat number 0.
Design a class that simulates the mentioned exam room.
Implement the ExamRoom class:
ExamRoom(int n) Initializes the object of the exam room with the number of the seats n.
int seat() Returns the label of the seat at which the next student will set.
void leave(int p) Indicates that the student sitting at seat p will leave the room. It is guaranteed that there will be a student sitting at seat p.
Explanation ExamRoom examRoom = new ExamRoom(10); examRoom.seat(); // return 0, no one is in the room, then the student sits at seat number 0. examRoom.seat(); // return 9, the student sits at the last seat number 9. examRoom.seat(); // return 4, the student sits at the last seat number 4. examRoom.seat(); // return 2, the student sits at the last seat number 2. examRoom.leave(4); examRoom.seat(); // return 5, the student sits at the last seat number 5.
Constraints:
1 <= n <= 10^9
It is guaranteed that there is a student sitting at seat p.
At most 10^4 calls will be made to seat and leave.
Solve
Heapq
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
(-9 , 0 , 9) 0123456789 -------------------------------- (-8 , 1 , 9 ) (1 , 0 , -1) v 0123456789 -------------------------------- (-7 , 1 , 8) v v 0123456789 -------------------------------- (-2 , 1 , 3) (-3 , 5 , 8) v v v 0123456789
-------------leave 4--------------- (-7 , 1 , 8) ~~(-2 , 1 , 3) (-3 , 5 , 8)~~ v v 0123456789
def__init__(self, n: int): self.n = n self.room = []
self.room.append((-self.dist(0, n - 1), 0, n - 1))
defseat(self) -> int: # 取出最長的空座位 dist , left , right = heapq.heappop(self.room)
if left == 0: res = 0 elif right == self.n - 1: res = self.n - 1 else: res = left - dist if res > left: heappush(self.room, (-self.dist(left, res - 1), left, res - 1)) if res < right: heappush(self.room, (-self.dist(res + 1, right), res + 1, right))
return res
defleave(self, p: int) -> None: pre_interval , next_interval = None , None left , right = p , p
for item in self.room: if item[2] + 1 == p : pre_interval = item
if item[1] - 1 == p : next_interval = item
if pre_interval: left = pre_interval[1] self.room.remove(pre_interval) if next_interval: right = next_interval[2] self.room.remove(next_interval)
# 找到兩個相鄰學生最遠距離 max_distance = self.room[0] - 0 index = 0
for i inrange(1 ,len(self.room)):
dist = ( self.room[i] - self.room[i-1] ) // 2
if dist > max_distance : max_distance = dist index = i # i-1 v i if self.n -1 - self.room[-1] > max_distance : res = self.n - 1 index = -1 elif index == 0: res = 0 else: res = ( self.room[index -1 ] + self.room[index] ) //2
for i inrange(1,len(self.room)): dist = self.room[i] - self.room[i-1]
if dist > max_distance : max_distance = dist index = i # i-1 v i if self.n -1 - self.room[-1] > max_distance : res = self.n - 1 index = -1 else: res = ( self.room[index -1 ] + self.room[index] ) //2