defbook(self, start, end): """ :type start: int :type end: int :rtype: bool """ find = False for i in range(len(self.schedule)): time = self.schedule[i] if start < time[1] and end > time[0]: returnFalse if time[0] > start: self.schedule.insert(i, (start, end)) find = True returnTrue ifnot find: self.schedule.append((start, end)) returnTrue
# Your MyCalendar object will be instantiated and called as such: # obj = MyCalendar() # param_1 = obj.book(start,end)
If we want to improve time complexity, we can use binary search and a tree