[JAVA] O(logN) seat/leave - TreeSet solution
\nclass ExamRoom {\n    \n    private final int max;\n    private final TreeSet<Interval> available;\n    private final TreeSet<Integer> taken;\n    \n    publi
```\nclass ExamRoom {\n    \n    private final int max;\n    private final TreeSet<Interval> available;\n    private final TreeSet<Integer> taken;\n    \n    public ExamRoom(int n) {\n        this.max = n - 1;\n        this.available = new TreeSet<>((a, b) -> {\n            var distA = getMinDistance(a);\n            var distB = getMinDistance(b);\n            return distA == distB ? a.s - b.s : distB - distA;\n        });\n        this.available.add(new Interval(0, max));\n        this.taken = new TreeSet<>();\n    }\n    \n    public int seat() {\n        var inter = available.pollFirst();\n        var idx = getInsertPosition(inter);\n        taken.add(idx);\n        if ((idx - 1) - inter.s >= 0)\n            available.add(new Interval(inter.s, idx - 1));\n        if (inter.e - (idx + 1) >= 0)\n            available.add(new Interval(idx + 1, inter.e));\n        return idx;\n    }\n    \n    public void leave(int p) {\n        taken.remove(p);\n        var lo = taken.lower(p);\n        if (lo == null)\n            lo = -1;\n        var hi = taken.higher(p);\n        if (hi == null)\n            hi = max + 1;\n        available.remove(new Interval(lo + 1, p - 1));\n        available.remove(new Interval(p + 1, hi - 1));\n        available.add(new Interval(lo + 1, hi - 1));\n    }\n                          \n    private int getInsertPosition(Interval inter) {\n        if (inter.s == 0)\n            return 0;\n        else if (inter.e == max)\n            return max;\n        else\n            return inter.s + (inter.e - inter.s) / 2;\n    }\n    \n    private int getMinDistance(Interval in) {\n        return in.s == 0 || in.e == max ? in.e - in.s : (in.e - in.s) / 2;\n    }\n    \n    private final class Interval {\n        private final int s;\n        private final int e;\n        \n        Interval(int s, int e) {\n            this.s = s;\n            this.e = e;\n        }\n        \n        @Override\n        public String toString() {\n            return "[" + s + "," + e + "]";\n        }\n    }\n}\n```
['Java']