My Elegant Java Solution Beats 99.84%
Like other posts, the seat() is O(logn) and leave() is O(n). The pq only save Intervals that are available for later use.\n\nclass ExamRoom {\n    class Interva
Like other posts, the `seat()` is O(logn) and `leave()` is O(n). The pq only save Intervals that are available for later use.\n```\nclass ExamRoom {\n    class Interval {\n        int start;\n        int end;\n        int length;\n        public Interval(int start, int end) {\n            this.start = start;\n            this.end = end;\n            if (start == 0 || end == N - 1) {\n                this.length = end - start;\n            } else {\n                this.length = (end - start) / 2;\n            }\n        }\n    }\n    private PriorityQueue<Interval> pq;\n    private int N;\n    \n    public ExamRoom(int N) {\n        this.pq = new PriorityQueue<>((a, b) -> a.length != b.length ? b.length - a.length : a.start - b.start);\n        this.N = N;\n        pq.offer(new Interval(0, N - 1));\n    }\n    \n    public int seat() {\n        Interval in = pq.poll();\n        int result;\n        if (in.start == 0) {\n            result = 0;\n        } else if (in.end == N - 1) {\n            result = N - 1;\n        } else {\n            result = in.start + in.length;\n        }\n        \n        if (result > in.start) {\n            pq.offer(new Interval(in.start, result - 1));   \n        }\n        if (in.end > result) {\n            pq.offer(new Interval(result + 1, in.end));   \n        }\n        return result;\n    }\n    \n    public void leave(int p) {\n        List<Interval> list = new ArrayList(pq);\n        Interval prev = null;\n        Interval next = null;\n        for (Interval in: list) {\n            if (in.end + 1 == p) {\n                prev = in;\n            }\n            if (in.start - 1 == p) {\n                next = in;\n            }\n        }\n        pq.remove(prev);\n        pq.remove(next);\n        pq.offer(new Interval(prev == null ? p : prev.start, next == null ? p : next.end));\n        \n    }\n}\n```
[]