Skip to content

Commit 483e91f

Browse files
authored
Create 731-my-calendar-ii.js
1 parent 64580d6 commit 483e91f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

731-my-calendar-ii.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const MyCalendarTwo = function () {
2+
this.calendar = []
3+
this.overlaps = []
4+
}
5+
6+
/**
7+
* @param {number} start
8+
* @param {number} end
9+
* @return {boolean}
10+
*/
11+
MyCalendarTwo.prototype.book = function (start, end) {
12+
for (let i = 0; i < this.overlaps.length; i++) {
13+
if (start < this.overlaps[i].end && end > this.overlaps[i].start)
14+
return false
15+
}
16+
17+
for (let i = 0; i < this.calendar.length; i++) {
18+
if (start < this.calendar[i].end && end > this.calendar[i].start)
19+
this.overlaps.push({
20+
start: Math.max(start, this.calendar[i].start),
21+
end: Math.min(end, this.calendar[i].end),
22+
})
23+
}
24+
this.calendar.push({ start: start, end: end })
25+
return true
26+
}
27+
28+
/**
29+
* Your MyCalendarTwo object will be instantiated and called as such:
30+
* var obj = new MyCalendarTwo()
31+
* var param_1 = obj.book(start,end)
32+
*/

0 commit comments

Comments
 (0)