Skip to content

Commit cc45ae0

Browse files
authored
Create 855-exam-room.js
1 parent d413c53 commit cc45ae0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

855-exam-room.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {number} n
3+
*/
4+
const ExamRoom = function(n) {
5+
let a = [];
6+
return { seat, leave }
7+
function seat() {
8+
if (a.length == 0) {
9+
a.push(0);
10+
return 0;
11+
}
12+
let dis = Math.max(a[0], n - 1 - a[a.length - 1]);
13+
for (let i = 1; i < a.length; i++) dis = Math.max(dis, a[i] - a[i - 1] >> 1);
14+
if (a[0] == dis) {
15+
a.unshift(0);
16+
return 0;
17+
}
18+
for (let i = 1; i < a.length; i++) {
19+
if (a[i] - a[i - 1] >> 1 == dis) {
20+
a.splice(i, 0, a[i] + a[i - 1] >> 1);
21+
return a[i];
22+
}
23+
}
24+
a.push(n - 1);
25+
return n - 1;
26+
}
27+
function leave(p) {
28+
for (let i = 0; i < a.length; i++) {
29+
if (a[i] == p) {
30+
a.splice(i, 1);
31+
break;
32+
}
33+
}
34+
}
35+
};

0 commit comments

Comments
 (0)