Skip to content

Commit 76b0b69

Browse files
authored
Create meeting-scheduler.py
1 parent d608221 commit 76b0b69

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Python/meeting-scheduler.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Time: O(n) ~ O(nlogn)
2+
# Space: O(n)
3+
4+
import heapq
5+
6+
7+
class Solution(object):
8+
def minAvailableDuration(self, slots1, slots2, duration):
9+
"""
10+
:type slots1: List[List[int]]
11+
:type slots2: List[List[int]]
12+
:type duration: int
13+
:rtype: List[int]
14+
"""
15+
min_heap = list(filter(lambda slot: slot[1] - slot[0] >= duration, slots1 + slots2))
16+
heapq.heapify(min_heap) # Time: O(n)
17+
while len(min_heap) > 1:
18+
left = heapq.heappop(min_heap) # Time: O(logn)
19+
right = min_heap[0]
20+
if left[1]-right[0] >= duration:
21+
return [right[0], right[0]+duration]
22+
return []
23+
24+
25+
# Time: O(nlogn)
26+
# Space: O(n)
27+
class Solution2(object):
28+
def minAvailableDuration(self, slots1, slots2, duration):
29+
"""
30+
:type slots1: List[List[int]]
31+
:type slots2: List[List[int]]
32+
:type duration: int
33+
:rtype: List[int]
34+
"""
35+
slots1.sort(key = lambda x: x[0])
36+
slots2.sort(key = lambda x: x[0])
37+
i, j = 0, 0
38+
while i < len(slots1) and j < len(slots2):
39+
left = max(slots1[i][0], slots2[j][0])
40+
right = min(slots1[i][1], slots2[j][1])
41+
if left+duration <= right:
42+
return [left, left+duration]
43+
if slots1[i][1] < slots2[j][1]:
44+
i += 1
45+
else:
46+
j += 1
47+
return []

0 commit comments

Comments
 (0)