- categories: Code, Interview Question, leetcode, Medium
- source: https://leetcode.com/problems/meeting-scheduler/
- topics: Two Pointers, Sorting
Given the availability time slots arrays slots1
and slots2
of two people and a meeting duration duration
, return the earliest time slot that works for both of them and is of duration duration
.
If there is no common time slot that satisfies the requirements, return an empty array.
The format of a time slot is an array of two elements [start, end]
representing an inclusive time range from start
to end
.
It is guaranteed that no two availability slots of the same person intersect with each other. That is, for any two time slots [start1, end1]
and [start2, end2]
of the same person, either start1 > end2
or start2 > end1
.
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]
"""
slots1.sort(key=lambda x: x[0])
slots2.sort(key=lambda x: x[0])
p1 = 0
p2 = 0
while p1 < len(slots1) and p2 < len(slots2):
intersection = [
max(slots1[p1][0], slots2[p2][0]),
min(slots1[p1][1], slots2[p2][1])
]
if intersection[1] - intersection[0] >= duration:
return [intersection[0], intersection[0] + duration]
if slots1[p1][1] < slots2[p2][1]:
p1 += 1
else:
p2 += 1
return []