~/DHRUVUpskilling
← board/DSA/merge Intervel/dsa-merge-intervel-04
Backlog·queued

Employee Free Time

DifficultyMedium
Patternmerge Intervel
TrackDSA
tl;dr

You’re given a list containing the schedules of multiple employees. Each person’s schedule is a list of non-overlapping intervals in sorted order. An interval is specified with the start and end time, both being positive integers. Your task is to find the list of finite intervals representing the free time for all the employees.

full write-up

Constraints

  • 1 ≤ schedule.length, schedule[i].length ≤ 50
  • 0 ≤ interval.start < interval.end ≤ 10⁸, where interval is any interval in the list of schedules.

Examples

Example 1

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

Explanation: Combining all intervals: [1, 3], [6, 7], [2, 4], [2, 5], [9, 12]. Merging overlapping intervals gives busy periods: [1, 5], [6, 7], [9, 12]. The gaps between these busy periods (excluding the very start and end) represent free time.

Output: [[5, 6], [7, 9]]

Example 2

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

Explanation: Combining all intervals: [1, 2], [5, 6], [1, 3], [4, 10]. Merging overlapping intervals gives busy periods: [1, 3], [4, 10]. The gap between these busy periods represents free time.

Output: [[3, 4]]