Skip to content

Commit 220fdaa

Browse files
authored
Create 729-my-calendar-i.js
1 parent 21c8b1e commit 220fdaa

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

729-my-calendar-i.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const MyCalendar = function () {
2+
this.root = null
3+
}
4+
5+
const Node = function (start, end) {
6+
this.start = start
7+
this.end = end
8+
this.left = null
9+
this.right = null
10+
}
11+
12+
Node.prototype.insert = function (node) {
13+
if (node.start >= this.end) {
14+
if (this.right === null) {
15+
this.right = node
16+
return true
17+
}
18+
return this.right.insert(node)
19+
} else if (node.end <= this.start) {
20+
if (this.left === null) {
21+
this.left = node
22+
return true
23+
}
24+
return this.left.insert(node)
25+
} else {
26+
return false
27+
}
28+
}
29+
30+
/**
31+
* @param {number} start
32+
* @param {number} end
33+
* @return {boolean}
34+
*/
35+
MyCalendar.prototype.book = function (start, end) {
36+
const newNode = new Node(start, end)
37+
if (this.root === null) {
38+
this.root = newNode
39+
return true
40+
} else {
41+
return this.root.insert(newNode)
42+
}
43+
}
44+
45+
/**
46+
* Your MyCalendar object will be instantiated and called as such:
47+
* var obj = new MyCalendar()
48+
* var param_1 = obj.book(start,end)
49+
*/

0 commit comments

Comments
 (0)