Skip to content

Commit c5c0c04

Browse files
authored
Create 732-my-calendar-iii.js
1 parent e2e87c0 commit c5c0c04

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

732-my-calendar-iii.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const find = (cals, time, count) => {
2+
let l = 0
3+
let r = cals.length
4+
let mid
5+
while (l < r) {
6+
mid = Math.trunc((l + r) / 2)
7+
if (cals[mid][0] === time) {
8+
cals[mid][1] += count
9+
return
10+
} else if (cals[mid][0] < time) {
11+
l = mid + 1
12+
} else {
13+
r = mid
14+
}
15+
}
16+
cals.splice(l, 0, [time, count])
17+
}
18+
const MyCalendarThree = function() {
19+
this.cals = []
20+
}
21+
22+
/**
23+
* @param {number} start
24+
* @param {number} end
25+
* @return {number}
26+
*/
27+
MyCalendarThree.prototype.book = function(start, end) {
28+
let idx = find(this.cals, start, 1)
29+
idx = find(this.cals, end, -1)
30+
let count = 0
31+
let max = 0
32+
for (let cal of this.cals) {
33+
count += cal[1]
34+
max = Math.max(max, count)
35+
}
36+
return max
37+
}
38+
39+
/**
40+
* Your MyCalendarThree object will be instantiated and called as such:
41+
* var obj = new MyCalendarThree()
42+
* var param_1 = obj.book(start,end)
43+
*/

0 commit comments

Comments
 (0)