Skip to the code
cf.
+50 XP

#586 Team-Wide Free Time

hard

A team calendar shows when nobody on the team is busy. schedules[p] is person p's list of busy blocks [start, end], where a block covers the time from start up to end. Lists can be in any order and blocks of different people overlap freely.

Write teamFreeTime(schedules) that returns every gap of positive length, between the earliest busy start and the latest busy end, when no one is busy. Return the gaps as [start, end] pairs sorted by start. If two busy blocks touch (one ends exactly when another starts), there's no gap between them.

Example 1

Input: schedules = [[[1,2],[5,6]],[[1,3]],[[4,10]]]
Output: [[3,4]]

Someone is busy from 1 to 3 and from 4 to 10, so the team is free from 3 to 4.

Example 2

Input: schedules = [[[1,3],[6,7]],[[2,4]],[[2,5],[9,12]]]
Output: [[5,6],[7,9]]

Busy time merges into [1, 5], [6, 7], and [9, 12].

Constraints

  • 0 <= schedules.length <= 50
  • 0 <= schedules[p].length <= 1000
  • 0 <= start < end <= 10^8
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).