Python3 Solution
Intuition\nFirst, initialize the heap with two fake seats, -1 and N. If we hit -1, we get the set 0. If we hit N, we get the set N-1. The main function is to us
# Intuition\nFirst, initialize the heap with two fake seats, -1 and N. If we hit -1, we get the set 0. If we hit N, we get the set N-1. The main function is to use a heap to organize the distance. We pop the minimum distance between two points each time if we need to add a person. We break the length into half and put this two half length into the heap. If we need to leave a person, we combine two lines into one and put them into the heap.\n\n# Code\n```\nclass ExamRoom:\n    \n    def dist(self, x, y):  # length of the interval (x, y)\n        if x == -1:        # note here we negate the value to make it maxheap\n            return -y\n        elif y == self.N:\n            return -(self.N - 1 - x)\n        else:\n            return -(abs(x-y)//2) \n        \n    def __init__(self, N):\n        self.N = N\n        self.pq = [(self.dist(-1, N), -1, N)]  # initialize heap\n        \n    def seat(self):\n        _, x, y = heapq.heappop(self.pq)  # current max interval \n        if x == -1:\n            seat = 0\n        elif y == self.N:\n            seat = self.N - 1\n        else:\n            seat = (x+y) // 2\n        heapq.heappush(self.pq, (self.dist(x, seat), x, seat))  # push two intervals by breaking at seat\n        heapq.heappush(self.pq, (self.dist(seat, y), seat, y))\n        return seat\n        \n    def leave(self, p):\n        head = tail = None\n        for interval in self.pq:  # interval is in the form of (d, x, y)\n            if interval[1] == p:  \n                tail = interval\n            if interval[2] == p:  \n                head = interval\n            if head and tail:\n                break\n        self.pq.remove(head)\n        self.pq.remove(tail)\n        heapq.heapify(self.pq)\n        heapq.heappush(self.pq, (self.dist(head[1], tail[2]), head[1], tail[2]))\n\n# Your ExamRoom object will be instantiated and called as such:\n# obj = ExamRoom(n)\n# param_1 = obj.seat()\n# obj.leave(p)\n```
['Python3']