Skip to content

Commit

Permalink
Update my-calendar-i.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Nov 20, 2017
1 parent 61059e2 commit 935e9fc
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions Python/my-calendar-i.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Time: O(n^2)
# Time: O(nlogn) on average, O(n^2) on worst case
# Space: O(n)

# Implement a MyCalendar class to store your events.
Expand Down Expand Up @@ -28,8 +28,50 @@
# Note:
# - The number of calls to MyCalendar.book per test case will be at most 1000.
# - In calls to MyCalendar.book(start, end), start and end are integers in the range [0, 10^9].


class Node:
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 = []
Expand Down

0 comments on commit 935e9fc

Please sign in to comment.