[Java] O(log n) for both operations using TreeSet
\nclass ExamRoom {\n    class Interval implements Comparable<Interval> {\n        public int L, R, dist, index;\n        Interval(int L, int R) {\n            t
```\nclass ExamRoom {\n    class Interval implements Comparable<Interval> {\n        public int L, R, dist, index;\n        Interval(int L, int R) {\n            this.L = L;\n            this.R = R;\n            if(L == 0) {\n                dist = R - L; index = L;\n            } else if(R == N - 1) {\n                dist = R - L; index = R;\n            } else {\n                dist = (R - L) >> 1; index = L + dist;\n            }\n        }\n        @Override\n        public int compareTo(Interval i) {\n            return dist == i.dist ? i.index - index : dist - i.dist;\n        }\n    }\n    private int N;\n    private TreeSet<Integer> seats = new TreeSet<>();\n    private TreeSet<Interval> intervals = new TreeSet<>();\n\n    public ExamRoom(int N) {\n        this.N = N;\n        intervals.add(new Interval(0, N-1));\n    }\n    \n    public int seat() {\n        Interval max = intervals.pollLast();\n        int index = max.index;\n        seats.add(index);\n        if(max.L <= index-1) {\n            intervals.add(new Interval(max.L, index-1));\n        }\n        if(index+1 <= max.R) {\n            intervals.add(new Interval(index+1, max.R));\n        }\n        return index;\n    }\n    \n    public void leave(int p) {\n        seats.remove(p);\n        Integer left = seats.floor(p), right = seats.ceiling(p);\n        int L = left == null ? 0 : left + 1, R = right == null ? N-1 : right - 1;\n        if(L <= p-1) {\n            intervals.remove(new Interval(L, p-1));\n        }\n        if(p+1 <= R) {\n            intervals.remove(new Interval(p+1, R));\n        }\n        intervals.add(new Interval(L, R));\n    }\n}\n```
[]