Skip to content

Latest commit

 

History

History
68 lines (49 loc) · 1.92 KB

_729. My Calendar I.md

File metadata and controls

68 lines (49 loc) · 1.92 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : September 26, 2024

Last updated : September 26, 2024


Related Topics : Array, Binary Search, Design, Segment Tree, Ordered Set

Acceptance Rate : 58.37 %


Solutions

Python

class MyCalendar:

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

    def book(self, start: int, end: int) -> bool:
        insertLocation = bisect_left(self.bookings, (start, end))

        # end of schedule
        if insertLocation >= len(self.bookings) :
            if not self.bookings :
                self.bookings.append((start, end))
                return True
            if start == self.bookings[-1][1] :
                self.bookings[-1] = (self.bookings[-1][0], end)
                return True
            if start < self.bookings[-1][1] :
                return False
            self.bookings.append((start, end))
            return True

        if insertLocation and start < self.bookings[insertLocation - 1][1] :
            return False
        if start == self.bookings[insertLocation][0] :
            return False
        if end == self.bookings[insertLocation][0] :
            self.bookings[insertLocation] = (start, self.bookings[insertLocation][1])
            return True
        if end > self.bookings[insertLocation][0] :
            return False

        self.bookings.insert(insertLocation, (start, end))
        return True



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