Given the availability time slots slots1 and slots2 of two people and a meeting duration, return the earliest time slot that works for both of them and has length duration.
If there is no common time slot that satisfies the requirements, return an empty list.
A time slot is a pair [start, end] representing an inclusive time range from start to end. No two availability slots of the same person intersect: for any two slots [start1, end1] and [start2, end2] of the same person, either start1 > end2 or start2 > end1.
Function Signature
def earliest_common_slot(slots1: list[list[int]], slots2: list[list[int]], duration: int) -> list[int]:
Rules
-
The length of a slot or of an overlap
[a, b]
is
b - a
.
-
A meeting
[s, s + duration]
works when it fits inside one slot of
slots1
and inside one slot of
slots2
.
-
Each person's slots may be given in any order.
-
Return the working meeting with the smallest start, as
[s, s + duration]
. Return
[]
when no meeting works.
Constraints
-
1 <= len(slots1), len(slots2) <= 10^4
-
Every slot has exactly two elements.
-
slots1[i][0] < slots1[i][1]
-
slots2[i][0] < slots2[i][1]
-
0 <= slots1[i][j], slots2[i][j] <= 10^9
-
1 <= duration <= 10^6
Examples
Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 8
Output: [60,68]
Both people are free during [10,15] and [60,70]. The first overlap has length 5, too short for 8. The second has length 10, so the meeting starts at 60 and ends at 68.
Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 12
Output: []
The overlaps are the same as in the first example, with lengths 5 and 10. Neither reaches 12.
Input: slots1 = [[30,40],[0,10]], slots2 = [[5,35]], duration = 5
Output: [5,10]
The common free windows are [5,10] and [30,35], each exactly 5 long, and an overlap whose length equals duration is enough. [30,40] is listed first in slots1, but [5,10] is the earlier window.