Practice

Employee Free Time

Module 21 · Intervals

Problem

You're given a list of schedules, one per employee — each schedule is a list of non-overlapping intervals already sorted for that one employee, representing when they are busy. Return a list of finite intervals representing the common free time for ALL employees, sorted, and excluding the unbounded free time before the first busy interval and after the last. (LeetCode 759.)

Examples

Example 1

Inputschedule = [[[1,2],[5,6]],[[1,3]],[[4,10]]]Output[[3,4]]

Example 2

Inputschedule = [[[1,3],[6,7]],[[2,4]],[[2,5],[9,12]]]Output[[5,6],[7,9]]

Flatten everyone's busy intervals and merge: [1,3], [4,10], [5,6] collapse to [1,3] and [4,10]. The only gap between those merged blocks is [3,4].

Constraints

total intervals across all employees up to ~10⁴.

Attempt it first

This is the module's capstone because it chains two ideas from earlier in this module (and Module 14) into one pipeline: first, treat every employee's busy intervals as just one big unsorted pool and MERGE them all — directly reusing Module 14's Merge Intervals logic — and then, once you have one clean, sorted, non-overlapping list of "everyone's combined busy time," the free time is exactly the GAPS between consecutive merged intervals. Before opening anything, convince yourself why merging across ALL employees together (not per-employee) is the right first step, and what "a gap between two sorted, non-overlapping intervals" looks like as a formula.