[Python] O(logn) both seat and leave. Easier to understand
The intuition is that, \n when to seat, use a heap to figure out the seat with max distance to neighbor.\n when to leave, use a seat to neighbor dict to get lef
The intuition is that, \n* when to seat, use a heap to figure out the seat with max distance to neighbor.\n* when to leave, use a seat to neighbor dict to get left and right neighbors, then update both neighbors pointing to each other, then remove this student from dict.\n\nOf course, we also need to update neighbors when a new student get seated. And we lazy delete the entry in the heap if the entry cannot be validated against the neighbor dict.\n\n```python\nclass ExamRoom:\n\n    def __init__(self, n: int):\n        self.n = n\n        self.h = [(self.dist(-1, n), -1, n)]\n        self.nei = {}\n        \n    def dist(self, l, r) -> int:\n        if l == -1:\n            return -r\n        elif r == self.n:\n            return -(self.n-1-l)\n        else:\n            return - ((r-l) // 2)\n\n    def seat(self) -> int:\n        n, h, nei = self.n, self.h, self.nei\n        while h:\n            _, left, right = heapq.heappop(h)\n            if (left != -1 and (left not in nei or nei[left][1] != right))\\\n                or (right != n and (right not in nei or nei[right][0] != left)):\n                continue\n            if left == -1:\n                nei[0] = [-1, right]\n                if right < n:\n                    nei[right][0] = 0\n                heapq.heappush(h, (self.dist(0, right), 0, right))\n                return 0\n            elif right == n:\n                nei[n-1] = [left, n]\n                nei[left][1] = n-1\n                heapq.heappush(h, (self.dist(left, n-1), left, n-1))\n                return n-1\n            else:\n                pos = (left+right) // 2\n                nei[left][1] = pos\n                nei[right][0] = pos\n                nei[pos] = [left, right]\n                heapq.heappush(h, (self.dist(left, pos), left, pos))\n                heapq.heappush(h, (self.dist(pos, right), pos, right))\n                return pos\n\n    def leave(self, p: int) -> None:\n        n, h, nei = self.n, self.h, self.nei\n        left, right = nei[p]\n        if left > -1:\n            nei[left][1] = right\n        if right < n:\n            nei[right][0] = left\n        heapq.heappush(h, (self.dist(left, right), left, right))\n        del nei[p]\n\n```
[]