Skip to main content

729 - My Calendar I

Details

KeyValue
Linkhttps://leetcode.com/problems/my-calendar-i/
LanguagePython 3
Runtime393 ms, faster than 62.19% of Python3 online submissions for My Calendar I
Memory Usage14.6 MB, less than 98.56% of Python3 online submissions for My Calendar I
DatastructuresList
AlgorithmsBinary Search
ComplexityTime: O(NlogN) Memory: O(N)

Procedure

  1. ...

Code

class MyCalendar:
def __init__(self):
self.events = []

def book(self, start: int, end: int) -> bool:
start_index = bisect.bisect_right(self.events, start)

# Start point must have even index
if start_index % 2 != 0: return False

end_index = bisect.bisect_left(self.events, end)

# Both start and end must have same index before insertion
if end_index != start_index: return False

bisect.insort_right(self.events, start)
bisect.insort_left(self.events, end)
return True

# Your MyCalendar object will be instantiated and called as such:
# obj = MyCalendar()
# param_1 = obj.book(start,end)