aboutsummaryrefslogtreecommitdiff
path: root/my_calendar2.py
blob: 77b2eee603c8c881c46e27655a4daaea9cd8a9e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 731. My Calendar II
import bisect

"""
you are implementing a program to use as your calendar. we can add a new
event if adding the event will not cause a triple booking. a triple booking
happens when three events have the same non empty intersection (ie. some
moment is common to all the three event). the event can be represented as a
pair of integers 'start' and 'end' that represents a booking on the half open
interval '[start, end]', the range of real numbers 'x' such that 'start <= x
< end'.
"""


class MyCalendarTwo(object):
    def __init__(self):
        self.list = []

    def book(self, start, end):
        """
        :type start: int
        :type end: int
        :rtype: bool
        """
        bisect.insort(self.list, (start, 1))
        bisect.insort(self.list, (end, -1))
        booked = 0
        for time, n in self.list:
            booked += n
            if booked == 3:
                self.list.pop(bisect.bisect_left(self.list, (start, 1)))
                self.list.pop(bisect.bisect_left(self.list, (end, -1)))
                return False
        return True


if __name__ == "__main__":
    obj = MyCalendarTwo()
    print(obj.book(10, 20))
    print(obj.book(50, 60))
    print(obj.book(10, 40))
    print(obj.book(5, 15))
    print(obj.book(5, 10))
    print(obj.book(25, 55))