All problems
0960MediumArrayTwo PointersSorting

Earliest Window Both Are Free

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1229Meeting Scheduler

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

Two people's free periods are given as slots1 and slots2, each entry a pair [start, end] meaning free from start up to end. A person's own periods never overlap each other.

Find the earliest moment t at which both are free for a stretch of duration, that is where the whole of t to t + duration lies inside one period of each.

Return [t, t + duration], or an empty list when no such moment exists.

Examples

Example 1

Input
slots1 = [[14, 27], [40, 55], [70, 90]], slots2 = [[20, 33], [50, 60], [80, 95]], duration = 8
Output
[80, 88]

The first overlap runs from 20 to 27, only seven long. The next runs from 50 to 55, five long. The one from 80 to 90 is ten long, so the stretch starts at 80.

Example 2

Input
slots1 = [[0, 5]], slots2 = [[5, 10]], duration = 1
Output
[]

The two periods meet at a single moment and overlap over nothing, so there is no room even for a stretch of one.

Example 3

Input
slots1 = [[10, 20], [0, 5]], slots2 = [[0, 30]], duration = 5
Output
[0, 5]

The periods are not given in order, and sorting them first shows the earliest workable overlap starts at 0.

Constraints

  • 1 <= slots1.length <= 10^4
  • 1 <= slots2.length <= 10^4
  • slots1[i].length == 2
  • slots2[i].length == 2
  • slots1[i][0] < slots1[i][1]
  • slots2[i][0] < slots2[i][1]
  • 0 <= slots1[i][j] <= 10^9
  • 0 <= slots2[i][j] <= 10^9
  • 1 <= duration <= 10^6
  • Neither person's own periods overlap each other

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def min_available_duration(slots1: list[list[int]], slots2: list[list[int]], duration: int) -> list[int]:
Java
public List<Integer> minAvailableDuration(int[][] slots1, int[][] slots2, int duration)
September 7
Apply