1
+ // Runtime: 466 ms (Top 38.09%) | Memory: 51 MB (Top 38.09%)
1
2
/**
2
3
* @param {number } n
3
4
*/
@@ -15,35 +16,35 @@ ExamRoom.prototype.seat = function() {
15
16
this . list . push ( 0 )
16
17
return 0
17
18
}
18
-
19
+
19
20
// find the largest distance between left wall and first student, and right wall and last student
20
21
let distance = Math . max ( this . list [ 0 ] , this . n - 1 - this . list [ this . list . length - 1 ] )
21
22
// update the largest distance by considering the distance between students
22
23
for ( let i = 0 ; i < this . list . length - 1 ; i ++ ) {
23
24
distance = Math . max ( distance , Math . floor ( ( this . list [ i + 1 ] - this . list [ i ] ) / 2 ) )
24
25
}
25
-
26
+
26
27
// in case the largest distance is between left wall and first student, we seat next student at the left wall
27
28
if ( distance === this . list [ 0 ] ) {
28
29
this . list . unshift ( 0 )
29
30
return 0
30
31
}
31
-
32
+
32
33
// in case the largest distance is between two student, we seat the next student in between these two students
33
34
for ( let i = 0 ; i < this . list . length - 1 ; i ++ ) {
34
35
if ( distance === Math . floor ( ( this . list [ i + 1 ] - this . list [ i ] ) / 2 ) ) {
35
36
let insertIndex = Math . floor ( ( this . list [ i + 1 ] + this . list [ i ] ) / 2 )
36
37
this . list . splice ( i + 1 , 0 , insertIndex )
37
38
return insertIndex
38
- }
39
+ }
39
40
}
40
-
41
+
41
42
// in case the largest distance is between the last student and the right wall, we seat the next student at the right wall
42
43
this . list . push ( this . n - 1 )
43
44
return this . n - 1
44
45
} ;
45
46
46
- /**
47
+ /**
47
48
* @param {number } p
48
49
* @return {void }
49
50
*/
@@ -57,9 +58,9 @@ ExamRoom.prototype.leave = function(p) {
57
58
}
58
59
} ;
59
60
60
- /**
61
+ /**
61
62
* Your ExamRoom object will be instantiated and called as such:
62
63
* var obj = new ExamRoom(n)
63
64
* var param_1 = obj.seat()
64
65
* obj.leave(p)
65
- */
66
+ */
0 commit comments