1
var SeatManager = function (n) {
2
this.min = 1;
3
this.n = n;
4
this.seat = [];
5
};
6

7
SeatManager.prototype.reserve = function () {
8
let res;
9

10
if (!this.seat[this.min]) {
11
res = this.min;
12
this.min = this.n < this.min ? NaN : this.min + 1;
13
} else {
14
res = this.min;
15
this.min = this.seat[this.min];
16
}
17

18
return res;
19
};
20

21
SeatManager.prototype.unreserve = function (seatNumber) {
22
if (this.min < seatNumber) {
23
let pre_inx = this.min;
24
let next_inx = this.seat[this.min];
25

26
while (next_inx < seatNumber) {
27
pre_inx = next_inx;
28
next_inx = this.seat[next_inx];
29
}
30

31
[this.seat[pre_inx], this.seat[seatNumber]] = [
32
seatNumber,
33
this.seat[pre_inx],
34
];
35
} else {
36
[this.seat[seatNumber], this.min] = [this.min, seatNumber];
37
}
38
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0