First joint slot: hints, no solution

Medium · Intervals

Two doctors want to meet on the same day. The day runs from minute 0 to minute 1440, and a meeting must fit completely inside it. Each doctor's busy periods are given as [start, end) pairs of minutes, in no particular order, and they may overlap within one list or between the two lists.

Return the earliest start minute of a meeting of exactly length minutes during which neither doctor is busy. A meeting may start at the moment a busy period ends and may end at the moment one starts. If no such slot exists, return -1.

Examples

Input
busyA = [[0, 600], [720, 780]], busyB = [[500, 660]], length = 60
Output
660
Note
Together the doctors are busy 0 to 660 and 720 to 780; the hour from 660 to 720 is free for both.
Input
busyA = [[0, 600], [720, 780]], busyB = [[500, 660]], length = 100
Output
780
Note
The gap from 660 to 720 is too short for 100 minutes, so the first fit starts at 780.
Input
busyA = [[0, 1440]], busyB = [], length = 30
Output
-1
Note
The first doctor is busy all day, so there is no joint slot.

Constraints

  • 0 <= busyA.length, busyB.length <= 500
  • 0 <= start < end <= 1440 for every busy period
  • 1 <= length <= 1440

Hint 1: a nudge

A meeting slot must be free for both doctors at once. What would the busy time look like if you treated the two lists as one calendar?

Unlock the full hints and timed practice with a pass

The second and third hints and the timed, runner-checked practice are part of a pass.

See pricing

Sign in

Check my resume first