C++ simple solution using vectors
You want to simulate an exam room where every next student seats furthest from its closest neighbour. So create a vector that will store all the seats filled un
You want to simulate an exam room where every next student seats furthest from its closest neighbour. So create a vector that will store all the seats filled until now. Idea is to find the maximum distance available between any two students and seat the current student in the middle of that gap.\nFor example, students seated at-> [0, 2, 5, 9] max distanct available between 5 and 9, so seat the student at 7.\nIn function ExamRoom, just initialise N.\nIn function seat, first see if no student is seated until now, if that\'s the case seat the current student at seat 0. Else go through the vector and check for all the consecutive student, if the maximum distance is available between the current pair. Update d, in every iteration. If the max distance is still between left extreme (0) and the students[0] (can be whatever index->think of case when 0 leaves), then return 0 directly after inserting 0 in students seat vector. Else again go through the students vector and see if that contained the maxium distance, if so, return the smallest seat available (we return value as soon as we get it). If until last we dont find our max distance, return N-1 and push the same in students. Notice that seats at 0 and N-1 positions need to be handled separately as they dont have any other value on extremes to pair with for calculations.\nIn function leave, just search for seat in the students vector and erase that seat.\n```\nclass ExamRoom {\n    vector<int> students;\n    int N;\npublic:\n    ExamRoom(int n) {\n        N = n;\n        students.clear();\n    }\n    \n    int seat() {\n        if(students.empty())\n        {\n            students.push_back(0);\n            return 0;\n        }\n        int d = max(students[0] - 0, N-1-students[students.size()-1]);\n        for(int i=0; i<students.size()-1; i++)\n            d = max(d, (students[i+1]- students[i])/2);\n        if(students[0]==d)\n        {\n            students.insert(students.begin(), 0);\n            return 0;\n        }\n        for(int i=0; i<students.size()-1; i++)\n        {\n            if((students[i+1]-students[i])/2==d)\n            {\n                students.insert(students.begin()+i+1, (students[i+1]+students[i])/2);\n                return students[i+1];\n            }\n        }\n        students.push_back(N-1);\n        return N-1;\n    }\n    \n    void leave(int p) {\n        for(int i=0; i<students.size(); i++)\n        {\n            if(students[i]==p)\n                students.erase(students.begin()+i);\n        }\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 ```\n Time complexity: O(N * M) Where N is maximum number of seat and leave request and M is the maximum size that vector students can expand to.\n Space complexity: O(M)
['C', 'C++']