aboutsummaryrefslogtreecommitdiff
path: root/my_calendar2.py
diff options
context:
space:
mode:
Diffstat (limited to 'my_calendar2.py')
-rw-r--r--my_calendar2.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/my_calendar2.py b/my_calendar2.py
new file mode 100644
index 0000000..77b2eee
--- /dev/null
+++ b/my_calendar2.py
@@ -0,0 +1,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))