Leetcode 729 Solution

This article provides solution to leetcode question 729 (my-calendar-i)

https://leetcode.com/problems/my-calendar-i

Solution

class MyCalendar:

    def __init__(self):
        self.books = []

    def book(self, start: int, end: int) -> bool:
        l = 0
        r = len(self.books) - 1
        while l < r:
            m = (l + r) // 2
            if self.books[m][0] >= end:
                r = m
            else:
                l = m + 1

        if not self.books:
            self.books.append((start, end))
            return True
        elif self.books[l][0] >= end:
            if l == 0 or self.books[l - 1][1] <= start:
                self.books.insert(l, (start, end))
                return True
            else:
                return False
        elif self.books[-1][1] <= start:
            self.books.insert(len(self.books), (start, end))
            return True
        else:
            return False


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