Leetcode # 729 (Python): My Calendar I
My Calendar I
Problem
Solution
# Create Binary tree
class Node:
def __init__(self,start,end):
self.start = start
self.end = end
self.right,self.left = None, 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:
def __init__(self):
self.root = None
def book(self, start: int, end: int) -> bool:
if not self.root:
self.root = Node(start,end)
return True
return self.root.insert(Node(start,end))
# Your MyCalendar object will be instantiated and called as such:
# obj = MyCalendar()
# param_1 = obj.book(start,end)
class MyCalendar:
def __init__(self):
self.calender = {}
def book(self, start: int, end: int) -> bool:
if not self.calender:
self.calender[start] = end
return True
for k in self.calender.keys():
if k < start and self.calender[k] > start:
return False
elif k > start and k < end:
return False
elif k == start:
return False
self.calender[start] = end
return True
# Your MyCalendar object will be instantiated and called as such:
# obj = MyCalendar()
# param_1 = obj.book(start,end)
Reference
この問題について(Leetcode # 729 (Python): My Calendar I), 我々は、より多くの情報をここで見つけました https://velog.io/@ahn16/Leetcode-729-Python-My-Calendar-Iテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol