C++ set solution with detailed comments
\nclass ExamRoom {\npublic:\n    \n    set<int> locations;\n    int numOfStudents;\n    \n    ExamRoom(int N):numOfStudents(N){\n    }\n    \n    int seat() {\n
```\nclass ExamRoom {\npublic:\n    \n    set<int> locations;\n    int numOfStudents;\n    \n    ExamRoom(int N):numOfStudents(N){\n    }\n    \n    int seat() {\n        int dist = 0;\n        int studentToSit = 0;\n        \n        //no students - just insert 0\n        if(!locations.empty()){\n            \n            //get the location of the first student\n            auto beg = locations.begin();\n            \n            //if this location is not 0\n            //first "attempt" to sit the student at 0\n            //we will record this for later and use this information when comparing other locations\n            if(*beg != 0){\n                dist = *beg;\n                studentToSit = 0;\n            }\n            \n            //go through all the students starting from the second seated student\n            for(auto i = next(beg); i != locations.end(); i = next(i)){\n                //get the distance between pair of students\n                int currDist = (*i - *beg)/2;\n\n                //if the distance is greater than the recorded distance thus far\n                //update the recorded distance and attempt to sit the student in the middle\n                if(currDist > dist){\n                    dist = currDist;\n                    studentToSit = *beg + dist;\n                }\n                \n                beg = i;\n            }\n            \n            \n            beg = prev(locations.end());\n            \n            //we also need to check the distance between the last student and\n            //the row size. If that distance is greater than the already recorded distance\n            //update the sit to be the last sit in the row\n            if(numOfStudents - *beg - 1 > dist){\n                studentToSit = numOfStudents - 1;\n            }\n        }\n        \n        //insert the new studen\'t sit\n        locations.insert(studentToSit);\n        return studentToSit;\n    }\n    \n    void leave(int p) {\n        locations.erase(p);\n    }\n};\n```
[]