4
var ExamRoom = function (n) {12
ExamRoom.prototype.seat = function () {13
// if nothing in the list, seat the first student at index 0.14
if (this.list.length === 0) {19
// find the largest distance between left wall and first student, and right wall and last student20
let distance = Math.max(22
this.n - 1 - this.list[this.list.length - 1]24
// update the largest distance by considering the distance between students25
for (let i = 0; i < this.list.length - 1; i++) {28
Math.floor((this.list[i + 1] - this.list[i]) / 2)32
// in case the largest distance is between left wall and first student, we seat next student at the left wall33
if (distance === this.list[0]) {38
// in case the largest distance is between two student, we seat the next student in between these two students39
for (let i = 0; i < this.list.length - 1; i++) {40
if (distance === Math.floor((this.list[i + 1] - this.list[i]) / 2)) {41
let insertIndex = Math.floor((this.list[i + 1] + this.list[i]) / 2);42
this.list.splice(i + 1, 0, insertIndex);47
// in case the largest distance is between the last student and the right wall, we seat the next student at the right wall48
this.list.push(this.n - 1);56
ExamRoom.prototype.leave = function (p) {57
// We iterate through the list and find the index where the student p sit at, then remove that index.58
for (let i = 0; i < this.list.length; i++) {59
if (this.list[i] === p) {60
this.list.splice(i, 1);67
* Your ExamRoom object will be instantiated and called as such:68
* var obj = new ExamRoom(n)69
* var param_1 = obj.seat()