题目:原题链接(中等)
标签:数组、有序映射
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
Ans 1 (Python) | O ( N 2 ) | O ( N ) | 380ms (50.66%) |
Ans 2 (Python) | |||
Ans 3 (Python) |
解法一:
class Node:
__slots__ = ["start", "end", "left", "right"]
def __init__(self, start, end):
self.start = start
self.end = end
self.left = 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:
def __init__(self):
self.root = None
def book(self, start: int, end: int) -> bool:
if self.root is None:
self.root = Node(start, end)
return True
else:
return self.root.insert(Node(start, end))