-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path729.py
50 lines (40 loc) · 1.73 KB
/
729.py
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
45
46
47
48
49
50
__________________________________________________________________________________________________
sample 220 ms submission
from bisect import bisect_right
class MyCalendar:
def __init__(self):
self.intervals = [[-2, -1], [10 ** 9 + 1, 10 ** 9 + 2]]
def book(self, start: int, end: int) -> bool:
curr = [start, 10 ** 9 + 1]
index = bisect_right(self.intervals, curr)
if(self.intervals[index - 1][1] > start or
self.intervals[index][0] < end):
return False
self.intervals.insert(index, [start, end])
return True
# Your MyCalendar object will be instantiated and called as such:
# obj = MyCalendar()
# param_1 = obj.book(start,end)
__________________________________________________________________________________________________
sample 13400 kb submission
class MyCalendar:
def __init__(self):
self.intervals = []
def book(self, start: int, end: int) -> bool:
if not self.intervals or end <= self.intervals[0][0]:
self.intervals = [[start, end]] + self.intervals
return True
if self.intervals[-1][1] <= start:
self.intervals.append([start, end])
return True
for ind in range(1, len(self.intervals)):
i = self.intervals[ind]
prev = self.intervals[ind - 1]
if start >= prev[1] and end <= i[0]:
self.intervals.insert(ind, [start, end])
return True
return False
# Your MyCalendar object will be instantiated and called as such:
# obj = MyCalendar()
# param_1 = obj.book(start,end)
__________________________________________________________________________________________________