My Calendar III LeetCode solution in JavaScript

1
var MyCalendarThree = function () {
2
this.intersections = [];
3
this.kEvents = 0;
4
};
5

6
/**
7
* @param {number} start
8
* @param {number} end
9
* @return {number}
10
*/
11
MyCalendarThree.prototype.book = function (start, end) {
12
let added = false;
13
for (let i = 0; i < this.intersections.length; i++) {
14
const a = this.intersections[i];
15
if (end <= a.start) {
16
this.intersections.splice(i, 0, { start, end, count: 1 });
17
this.kEvents = Math.max(this.kEvents, 1);
18
this.added = true;
19
break;
20
}
21
if (start < a.start) {
22
this.intersections.splice(i, 0, { start, end: a.start, count: 1 });
23
i++;
24
start = a.start;
25
}
26
if (a.start < start && start < a.end) {
27
this.intersections.splice(i, 0, {
28
start: a.start,
29
end: start,
30
count: a.count,
31
});
32
i++;
33
a.start = start;
34
}
35
if (end < a.end) {
36
this.intersections.splice(i + 1, 0, {
37
start: end,
38
end: a.end,
39
count: a.count,
40
});
41
a.count++;
42
a.end = end;
43
this.kEvents = Math.max(this.kEvents, a.count);
44
this.added = true;
45
break;
46
}
47
if (end === a.end) {
48
a.count++;
49
a.end = end;
50
this.kEvents = Math.max(this.kEvents, a.count);
51
this.added = true;
52
break;
53
}
54
if (a.start === start && a.end < end) {
55
a.count++;
56
this.kEvents = Math.max(this.kEvents, a.count);
57
start = a.end;
58
}
59
}
60
if (!added) {
61
this.intersections.push({ start, end, count: 1 });
62
this.kEvents = Math.max(this.kEvents, 1);
63
}
64
return this.kEvents;
65
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0