Java hashmap and treeset solution faster than 81% with seat O(logN) and leave O(1))
\nclass ExamRoom {\n\n    int count = 0;\n    int capability;\n    TreeSet<int[]> pq; \n    Map<Integer, Pair<Integer, Integer>> map;  // key is seat itself, va
```\nclass ExamRoom {\n\n    int count = 0;\n    int capability;\n    TreeSet<int[]> pq; \n    Map<Integer, Pair<Integer, Integer>> map;  // key is seat itself, value is pair which record its\' left and right neighbors\n    \n    public ExamRoom(int N) {\n        this.capability = N;\n        this.count = 0;\n        this.pq = new TreeSet<int[]>((a ,b)-> {\n              int len_a = (a[1]-a[0])/2;\n              int len_b = (b[1]-b[0])/2;\n              if (a[0] == -1 || a[1] == capability) {\n                  len_a = a[1] - a[0] - 1;\n              }\n              if (b[0] == -1 || b[1] == capability) {\n                  len_b = b[1] - b[0] - 1;\n              }\n              if (len_b - len_a != 0) {\n                  return len_b - len_a;\n              } else {\n                  return (a[0] - b[0]);\n              }\n            });\n        this.map = new HashMap<>();\n        pq.add(new int[] {-1, capability}); \n    }\n    \n    public int seat() {\n        count++;\n        int[] seatsPair = pq.pollFirst();\n        int left = seatsPair[0];\n        int right = seatsPair[1];\n        Pair<Integer, Integer> leftP = map.get(left);\n        Pair<Integer, Integer> rightP = map.get(right);\n        if (left == -1) {\n            map.put(0, new Pair<Integer,Integer>(-1, right));\n            if (right != capability)\n                map.put(right, new Pair<Integer,Integer>(0, rightP.getValue()));\n            pq.add(new int[] {0, right});    // first choice\n            return 0;\n        } else if (right == capability) {\n            map.put(capability-1, new Pair<Integer,Integer>(left, capability));\n            map.put(left, new Pair<Integer,Integer>(leftP.getKey(), capability-1));\n            pq.add(new int[] {left, capability-1});  // second choice\n            return capability-1;\n        } else {\n            int pos = left + (right - left) / 2;\n            map.put(pos, new Pair<>(left, right));\n            map.put(left, new Pair<>(leftP.getKey(), pos));\n            map.put(right, new Pair<>(pos, rightP.getValue())); \n            pq.add(new int[] {left, pos});\n            pq.add(new int[] {pos, right});\n            return pos;\n        }\n    }\n    \n    public void leave(int p) {\n        count--;\n        Pair<Integer,Integer> pair = map.get(p);\n        int pos1 = pair.getKey(); \n        int pos2 = pair.getValue();\n        Pair<Integer, Integer> leftP = map.get(pos1);\n        Pair<Integer, Integer> rightP = map.get(pos2);\n        map.remove(p);\n        if (leftP != null)\n            map.put(pos1, new Pair<Integer, Integer>(leftP.getKey(), pos2));\n        if (rightP != null)\n            map.put(pos2, new Pair<Integer, Integer>(pos1, rightP.getValue()));\n        pq.remove(new int[] {pos1, p});\n        pq.remove(new int[] {p, pos2});\n        pq.add(new int[] {pos1, pos2});\n    }\n}\n```
[]