forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmy-calendar-iii.py
38 lines (27 loc) · 917 Bytes
/
my-calendar-iii.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
# Time: O(n^2)
# Space: O(n)
import bisect
class MyCalendarThree(object):
def __init__(self):
self.__books = []
def book(self, start, end):
"""
:type start: int
:type end: int
:rtype: int
"""
i = bisect.bisect_left(self.__books, (start, 1))
if i < len(self.__books) and self.__books[i][0] == start:
self.__books[i] = (self.__books[i][0], self.__books[i][1]+1)
else:
self.__books.insert(i, (start, 1))
j = bisect.bisect_left(self.__books, (end, 1))
if j < len(self.__books) and self.__books[j][0] == end:
self.__books[j] = (self.__books[j][0], self.__books[j][1]-1)
else:
self.__books.insert(j, (end, -1))
result, cnt = 0, 0
for book in self.__books:
cnt += book[1]
result = max(result, cnt)
return result