Employee Free Time - Problem
We are given a list schedule of employees, which represents the working time for each employee. Each employee has a list of non-overlapping Intervals, and these intervals are in sorted order.
Return the list of finite intervals representing common, positive-length free time for all employees, also in sorted order.
Note: Even though we are representing Intervals in the form [x, y], the objects inside are Intervals, not lists or arrays. For example, schedule[0][0].start = 1, schedule[0][0].end = 2, and schedule[0][0][0] is not defined. Also, we wouldn't include intervals like [5, 5] in our answer, as they have zero length.
Input & Output
Example 1 — Basic Case
$
Input:
schedule = [[[1,3],[6,10]], [[2,4],[9,12]]]
›
Output:
[[4,6]]
💡 Note:
Employee 1 works [1,3] and [6,10]. Employee 2 works [2,4] and [9,12]. Merging all intervals: [1,4] and [6,12]. The gap between them is [4,6].
Example 2 — Multiple Gaps
$
Input:
schedule = [[[1,3],[4,6]], [[2,5],[7,9]]]
›
Output:
[[6,7]]
💡 Note:
Employee 1: [1,3],[4,6]. Employee 2: [2,5],[7,9]. After merging: [1,6] and [7,9]. Free time is [6,7].
Example 3 — No Free Time
$
Input:
schedule = [[[1,10]], [[2,6],[8,12]]]
›
Output:
[]
💡 Note:
Employee 1 works [1,10], Employee 2 works [2,6],[8,12]. Combined coverage is [1,12] with no gaps.
Constraints
- 1 ≤ schedule.length ≤ 50
- 1 ≤ schedule[i].length ≤ 50
- schedule[i][j].length == 2
- 0 ≤ schedule[i][j][0] < schedule[i][j][1] ≤ 108
Visualization
Tap to expand
Understanding the Visualization
1
Input
Multiple employee schedules with working intervals
2
Process
Merge all busy intervals to find covered time
3
Output
Gaps between merged intervals are free time
Key Takeaway
🎯 Key Insight: Free time is simply the gaps between merged busy intervals from all employees
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code