Earliest Shared Free Slot Across Several Schedules
Greedy & IntervalsAuto-graded · 23 test casesLast updated
Find the earliest meeting slot of a given length that fits inside everyone's availability window and avoids every busy block, by intersecting windows and merging busy intervals.
Earliest Shared Free Slot Across Several Schedules
A group of people wants to meet. Person i has an availability window windows[i] = [open_i, close_i] and a list of busy blocks busy_lists[i], where each block is [begin, finish]. All times are integer minutes after midnight.
Every interval is half-open: a block [begin, finish] occupies the minutes from begin up to, but not including, finish. A meeting that starts exactly when a busy block finishes is fine, and so is a meeting that ends exactly when a busy block begins.
Find the earliest meeting [t, t + length] such that:
- it lies inside every person's window, that is
t >= open_iandt + length <= close_ifor alli, and - it does not overlap any busy block of any person.
"Earliest" means the smallest possible t.
Input
busy_lists: a list with one entry per person. Each entry is a list of[begin, finish]busy blocks. The blocks of one person may be unsorted, may overlap or nest, and may extend outside that person's window.windows: a list of[open, close]pairs, one per person, in the same order asbusy_lists.length: the required meeting length in minutes.
Output
Return [t, t + length] for the earliest valid meeting, or [] if there is none.
Rules and edge cases
- If
length < 0, orbusy_listsandwindowshave different lengths, return[]. - If there are no people, return
[]. - Let
startbe the largestopen_iandendbe the smallestclose_i. Ifend - start < length, return[]. - If
length == 0(and the check above passed), return[start, start]. - A busy block with
begin >= finishoccupies no time and is ignored. Likewise, any part of a block outside[start, end]is irrelevant. - Back-to-back blocks such as
[60, 90]and[90, 120]leave a gap of zero minutes, which can only host a meeting of length 0. - A meeting may exactly fill a gap, including the final gap that ends at
end.
Constraints
0 <= number of people <= 10^4- Total number of busy blocks across all people is at most
2 * 10^5 0 <= open_i <= close_i <= 10^9(the values are not required to fit in a single day)- Busy block endpoints are integers in the range
[-10^9, 10^9] -10^9 <= length <= 10^9
Examples
Example 1
Input: find_first_shared_slot([[[60, 120], [180, 240]], [[90, 150]], [[150, 180]]], [[0, 300], [0, 300], [0, 300]], 30)
Output: [0, 30]
Example 2
Input: find_first_shared_slot([[[30, 60]], [[60, 90]]], [[0, 120], [0, 120]], 30)
Output: [0, 30]
Example 3
Input: find_first_shared_slot([[], []], [[0, 480], [60, 420]], 60)
Output: [60, 120]
Hints
Hint 1
Anyone being busy blocks the minute for everybody, so the owner of a busy block does not matter. Only the union of all busy time matters.
Hint 2
Intersect the windows first: the meeting must fit in [max of opens, min of closes]. Clip every busy block to that range and drop the empty ones.
Hint 3
Sort the clipped blocks by start and sweep with a pointer 'cursor' (the earliest time not yet blocked). If the next block starts at least `length` after the cursor, you found the answer; otherwise move the cursor to max(cursor, block end). Remember to check the tail gap after the last block.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The task reduces to two simple operations.
- Intersect the windows. A valid meeting must lie in
[start, end], wherestartis the maximum of all opening times andendis the minimum of all closing times. Ifend - start < length, nothing can fit, so return[]. Handle the invalid-input cases andlength == 0exactly as the statement describes. - Union the busy time. A minute is unusable if any person is busy then, so the owner of each block is irrelevant. Clip every block to
[start, end], discard empty ones, and sort all blocks together by start time.
Then sweep through the sorted blocks while keeping a cursor, the earliest minute not yet known to be blocked (initially start):
- If the next block begins at least
lengthminutes aftercursor, the gap[cursor, block start)fits the meeting. Return[cursor, cursor + length]. - Otherwise the block (possibly overlapping earlier ones) pushes the cursor to
max(cursor, block end). - If the remaining room
end - cursordrops belowlength, return[]early.
After the last block, check the final gap up to end.
Why it is correct
Blocks are processed in non-decreasing start order, so when a block starting at s is reached, no unseen block starts before s. Everything in [cursor, s) is therefore free for everybody, and the cursor never skips a free minute. The first gap that is long enough gives the smallest valid start, namely its left edge. Using max(cursor, right) absorbs overlapping or nested blocks without any pre-merging. Half-open semantics make the >= comparisons exact, so touching blocks leave zero-length gaps and a meeting may exactly fill a gap.
Complexity
Let N be the total number of busy blocks and K the number of people.
- Time:
O(K + N log N), dominated by sorting. - Space:
O(N)for the clipped blocks.
Reference solution
from typing import List
def find_first_shared_slot(busy_lists: List[List[List[int]]], windows: List[List[int]], length: int) -> List[int]:
if length < 0 or len(busy_lists) != len(windows):
return []
if not busy_lists:
return []
start = max(w[0] for w in windows)
end = min(w[1] for w in windows)
if end - start < length:
return []
if length == 0:
return [start, start]
blocks = []
for person in busy_lists:
for b in person:
left = max(b[0], start)
right = min(b[1], end)
if left < right:
blocks.append((left, right))
blocks.sort()
cursor = start
for left, right in blocks:
if left - cursor >= length:
return [cursor, cursor + length]
cursor = max(cursor, right)
if end - cursor < length:
return []
if end - cursor >= length:
return [cursor, cursor + length]
return []