Employee Free Time
Problem statement
You are given the work schedules of several employees. schedule[i] is the list of time intervals when employee i is working. An interval [start, end] means the employee is busy from time start until time end.
Each employee's intervals are sorted by start time and do not overlap. One interval may end at the same time as the next one starts.
Find every stretch of time when all employees are free at the same time. Each free stretch must:
- lie between two busy intervals. The open-ended time before the earliest start and after the latest end does not count.
- have positive length. If one interval ends at time
tand another starts at timet, there is no free time between them. - be as long as possible. It starts when the busy time before it ends and stops when the next busy time starts.
Return the free stretches as [start, end] pairs sorted by start time. If there is no common free time, return an empty array.
Function
employeeFreeTime(schedule: int[][][]) → int[][]Examples
Example 1
schedule = [[[1,3],[8,10]],[[2,5]],[[9,12]]]return = [[5,8]]Taken together, the employees are busy from 1 to 5 (intervals [1,3] and [2,5] overlap) and from 8 to 12 (intervals [8,10] and [9,12] overlap). Nobody works between 5 and 8.
Example 2
schedule = [[[0,2],[4,6],[10,11]],[[1,3],[7,8]]]return = [[3,4],[6,7],[8,10]]The busy stretches are [0,3], [4,6], [7,8] and [10,11]. The gaps between them are [3,4], [6,7] and [8,10].
Example 3
schedule = [[[2,4],[4,7]],[[3,5]]]return = []Employee 0 stops working at time 4 and starts again at time 4, so there is no gap there. Everyone is covered from 2 to 7, so there is no common free time.
Constraints
1 <= schedule.length <= 501 <= schedule[i].length <= 50schedule[i][j].length == 20 <= schedule[i][j][0] < schedule[i][j][1] <= 10^8schedule[i][j][1] <= schedule[i][j + 1][0]for every employeeiand everyjwith0 <= j < schedule[i].length - 1