KatInterview

Earliest Shared Free Slot Across Several Schedules

Greedy & IntervalsAuto-graded · 23 test cases
MediumAuto-gradedGreedy & Intervalsintervalssweep-linesorting

Last 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_i and t + length <= close_i for all i, 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 as busy_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, or busy_lists and windows have different lengths, return [].
  • If there are no people, return [].
  • Let start be the largest open_i and end be the smallest close_i. If end - start < length, return [].
  • If length == 0 (and the check above passed), return [start, start].
  • A busy block with begin >= finish occupies 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.