Simple TreeSet solution with reasoning
Intuition\n Describe your first thoughts on how to solve this problem. \nTreeSet can give us lower and higher elements in log n time. We utilise the same data s
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTreeSet can give us lower and higher elements in log n time. We utilise the same data structure here.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. If no seats are occupied sit at 0\n2. Else find all compute the mid point between every consecutive occupied seat and check if distance is max or not.\n3. If there is an elemnt already at position 0, the next round of seating should be at position N-1.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nIteration for each position takes : O(m) where m is size of occupied seats\nAnd then we add the element to seats set. 0(log m).\nSo total:  O(m+log(m))\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(m) where m is total occupied seats\n\n# Code\n```java []\nclass ExamRoom {\n    TreeSet<Integer> seats;\n    int n;\n    public ExamRoom(int n) {\n        this.seats = new TreeSet<>();\n        this.n = n;\n    }\n    \n    public int seat() {\n        if(seats.size() == 0){\n            seats.add(0);\n            return 0;\n        }\n        \n        int seatToSit = 0;\n        int maxDist = seats.first();\n        int prev = Integer.MIN_VALUE;\n        for(int curr : seats){\n            if(prev != Integer.MIN_VALUE){\n                int newElementDist = (curr - prev) / 2;\n                if(newElementDist > maxDist){\n                    maxDist = newElementDist;\n                    int newSeat = prev + newElementDist;\n                    seatToSit = newSeat;\n                }\n            }\n            prev = curr;\n        }\n        if(n-1-seats.last() > maxDist){\n            seatToSit = n-1;\n        }\n        seats.add(seatToSit);\n        return seatToSit;\n    }\n    \n    public void leave(int p) {\n        seats.remove(p);\n    }\n}\n\n/**\n * Your ExamRoom object will be instantiated and called as such:\n * ExamRoom obj = new ExamRoom(n);\n * int param_1 = obj.seat();\n * obj.leave(p);\n */\n```
['Java']