Skip to content

Commit a35704c

Browse files
committed
Runtime: 466 ms (Top 38.09%) | Memory: 51 MB (Top 38.09%)
1 parent cda892a commit a35704c

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

scripts/algorithms/E/Exam Room/Exam Room.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Runtime: 466 ms (Top 38.09%) | Memory: 51 MB (Top 38.09%)
12
/**
23
* @param {number} n
34
*/
@@ -15,35 +16,35 @@ ExamRoom.prototype.seat = function() {
1516
this.list.push(0)
1617
return 0
1718
}
18-
19+
1920
// find the largest distance between left wall and first student, and right wall and last student
2021
let distance = Math.max(this.list[0], this.n - 1 - this.list[this.list.length-1])
2122
// update the largest distance by considering the distance between students
2223
for(let i=0; i<this.list.length-1; i++){
2324
distance = Math.max(distance, Math.floor((this.list[i+1] - this.list[i]) / 2))
2425
}
25-
26+
2627
// in case the largest distance is between left wall and first student, we seat next student at the left wall
2728
if(distance === this.list[0]){
2829
this.list.unshift(0)
2930
return 0
3031
}
31-
32+
3233
// in case the largest distance is between two student, we seat the next student in between these two students
3334
for(let i=0; i<this.list.length-1; i++){
3435
if(distance === Math.floor( (this.list[i+1]-this.list[i])/2 )){
3536
let insertIndex = Math.floor( (this.list[i+1]+this.list[i]) / 2 )
3637
this.list.splice(i+1,0, insertIndex)
3738
return insertIndex
38-
}
39+
}
3940
}
40-
41+
4142
// in case the largest distance is between the last student and the right wall, we seat the next student at the right wall
4243
this.list.push(this.n-1)
4344
return this.n - 1
4445
};
4546

46-
/**
47+
/**
4748
* @param {number} p
4849
* @return {void}
4950
*/
@@ -57,9 +58,9 @@ ExamRoom.prototype.leave = function(p) {
5758
}
5859
};
5960

60-
/**
61+
/**
6162
* Your ExamRoom object will be instantiated and called as such:
6263
* var obj = new ExamRoom(n)
6364
* var param_1 = obj.seat()
6465
* obj.leave(p)
65-
*/
66+
*/

0 commit comments

Comments
 (0)