Problem description:

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.

Example 1:

1
2
Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 8
Output: [60,68]

Example 2:

1
2
Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 12
Output: []

Constraints:

  • 1 <= slots1.length, slots2.length <= 104
  • slots1[i].length, slots2[i].length == 2
  • slots1[i][0] < slots1[i][1]
  • slots2[i][0] < slots2[i][1]
  • 0 <= slots1[i][j], slots2[i][j] <= 109
  • 1 <= duration <= 106

Solution:

Iterate until i reaches the end of slots1 or j reaches the end of slots2:
Find the intersect slot between slots1[i] and slots2[j]
If the intersect slot >= duration, return the result.
Else, find the slot that ends earlier and move the pointer.
If no result is found, return an empty array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
def minAvailableDuration(self, slots1: List[List[int]], slots2: List[List[int]], duration: int) -> List[int]:
p1, p2 = 0, 0
slots1.sort()
slots2.sort()
res = []
while p1 < len(slots1) and p2 < len(slots2):
start = max(slots1[p1][0], slots2[p2][0])
end = min(slots1[p1][1], slots2[p2][1])
if end - start >= duration:
return [start, start+duration]
if slots1[p1][1] > slots2[p2][1]:
p2 += 1
else:
p1 += 1
return []

time complexity: $O(MlogM+ NlogN)$,
space complexity: $O()$
reference:
related problem: