forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meeting-scheduler.py
47 lines (42 loc) · 1.39 KB
/
meeting-scheduler.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
# Time: O(n) ~ O(nlogn)
# Space: O(n)
import heapq
class Solution(object):
def minAvailableDuration(self, slots1, slots2, duration):
"""
:type slots1: List[List[int]]
:type slots2: List[List[int]]
:type duration: int
:rtype: List[int]
"""
min_heap = list(filter(lambda slot: slot[1] - slot[0] >= duration, slots1 + slots2))
heapq.heapify(min_heap) # Time: O(n)
while len(min_heap) > 1:
left = heapq.heappop(min_heap) # Time: O(logn)
right = min_heap[0]
if left[1]-right[0] >= duration:
return [right[0], right[0]+duration]
return []
# Time: O(nlogn)
# Space: O(n)
class Solution2(object):
def minAvailableDuration(self, slots1, slots2, duration):
"""
:type slots1: List[List[int]]
:type slots2: List[List[int]]
:type duration: int
:rtype: List[int]
"""
slots1.sort(key = lambda x: x[0])
slots2.sort(key = lambda x: x[0])
i, j = 0, 0
while i < len(slots1) and j < len(slots2):
left = max(slots1[i][0], slots2[j][0])
right = min(slots1[i][1], slots2[j][1])
if left+duration <= right:
return [left, left+duration]
if slots1[i][1] < slots2[j][1]:
i += 1
else:
j += 1
return []