JavaScript Simple O(n) Seat and Leave Solution With Detailed Comments
\n/**\n * @param {number} n\n */\nvar ExamRoom = function(n) {\n    this.n = n\n    this.list = []\n};\n\n/**\n * @return {number}\n */\nExamRoom.prototype.seat
```\n/**\n * @param {number} n\n */\nvar ExamRoom = function(n) {\n    this.n = n\n    this.list = []\n};\n\n/**\n * @return {number}\n */\nExamRoom.prototype.seat = function() {\n    // if nothing in the list, seat the first student at index 0.\n    if(this.list.length === 0){\n        this.list.push(0)\n        return 0\n    }\n    \n    // find the largest distance between left wall and first student, and right wall and last student\n    let distance = Math.max(this.list[0], this.n - 1 - this.list[this.list.length-1])\n    // update the largest distance by considering the distance between students\n    for(let i=0; i<this.list.length-1; i++){\n        distance = Math.max(distance, Math.floor((this.list[i+1] - this.list[i]) / 2))\n    }\n    \n    // in case the largest distance is between left wall and first student, we seat next student at the left wall\n    if(distance === this.list[0]){\n        this.list.unshift(0)\n        return 0\n    }\n    \n    // in case the largest distance is between two student, we seat the next student in between these two students\n    for(let i=0; i<this.list.length-1; i++){\n        if(distance === Math.floor( (this.list[i+1]-this.list[i])/2 )){\n            let insertIndex = Math.floor( (this.list[i+1]+this.list[i]) / 2 )\n            this.list.splice(i+1,0, insertIndex)\n            return insertIndex\n        }   \n    }\n    \n    // in case the largest distance is between the last student and the right wall, we seat the next student at the right wall\n    this.list.push(this.n-1)\n    return this.n - 1\n};\n\n/** \n * @param {number} p\n * @return {void}\n */\nExamRoom.prototype.leave = function(p) {\n    // We iterate through the list and find the index where the student p sit at, then remove that index.\n    for(let i=0; i<this.list.length; i++){\n        if(this.list[i] === p) {\n            this.list.splice(i, 1)\n            break\n        }\n    }\n};\n\n/** \n * Your ExamRoom object will be instantiated and called as such:\n * var obj = new ExamRoom(n)\n * var param_1 = obj.seat()\n * obj.leave(p)\n */\n```
['JavaScript']