-
Notifications
You must be signed in to change notification settings - Fork 43
/
my-calendar-ii.py
57 lines (50 loc) · 1.42 KB
/
my-calendar-ii.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
51
52
53
54
55
56
57
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/82820204
# IDEA : GREEDY
class MyCalendarTwo(object):
def __init__(self):
# every booked interval
self.booked = list()
# every overlap interval
self.overlaped = list()
def book(self, start, end):
"""
:type start: int
:type end: int
:rtype: bool
"""
for os, oe in self.overlaped:
if max(os, start) < min(oe, end):
return False
for bs, be in self.booked:
ss = max(bs, start)
ee = min(be, end)
if ss < ee:
self.overlaped.append((ss, ee))
self.booked.append((start, end))
return True
# Your MyCalendarTwo object will be instantiated and called as such:
# obj = MyCalendarTwo()
# param_1 = obj.book(start,end)
# V2
# Time: O(n^2)
# Space: O(n)
class MyCalendarTwo(object):
def __init__(self):
self.__overlaps = []
self.__calendar = []
def book(self, start, end):
"""
:type start: int
:type end: int
:rtype: bool
"""
for i, j in self.__overlaps:
if start < j and end > i:
return False
for i, j in self.__calendar:
if start < j and end > i:
self.__overlaps.append((max(start, i), min(end, j)))
self.__calendar.append((start, end))
return True