forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
my-calendar-i.py
65 lines (50 loc) · 1.39 KB
/
my-calendar-i.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Time: O(nlogn) on average, O(n^2) on worst case
# Space: O(n)
class Node(object):
def __init__(self, start, end):
self.__start = start
self.__end = end
self.__left = None
self.__right = None
def insert(self, node):
if node.__start >= self.__end:
if not self.__right:
self.__right = node
return True
return self.__right.insert(node)
elif node.__end <= self.__start:
if not self.__left:
self.__left = node
return True
return self.__left.insert(node)
else:
return False
class MyCalendar(object):
def __init__(self):
self.__root = None
def book(self, start, end):
"""
:type start: int
:type end: int
:rtype: bool
"""
if self.__root is None:
self.__root = Node(start, end)
return True
return self.root.insert(Node(start, end))
# Time: O(n^2)
# Space: O(n)
class MyCalendar2(object):
def __init__(self):
self.__calendar = []
def book(self, start, end):
"""
:type start: int
:type end: int
:rtype: bool
"""
for i, j in self.__calendar:
if start < j and end > i:
return False
self.__calendar.append((start, end))
return True