You are given schedule, a list where each element contains the working hours of one employee.
Each employee’s working hours are represented as a list of Interval objects that are already sorted and do not overlap.
Return all finite intervals with non-zero duration during which all employees are simultaneously free. The resulting list of free intervals should also be sorted in ascending order.
Note: The intervals are represented as objects, not arrays. For example,
schedule[1][1].start = 1andschedule[1][1].end = 2, whileschedule[0][0][0]is invalid. Do not include intervals with zero length (for example,[3, 3]) in the output.
Constraints:
1≤ schedule.length , schedule[i].length ≤50
0≤ interval.start < interval.end ≤108, where interval is any interval in the list of schedules.
The solution’s core revolves around merging overlapping intervals of employees and identifying the free time gaps between these merged intervals. Using a min-heap, we arrange the intervals based on when they start, sorting them according to their start times. When we pop an element from the min-heap, it guarantees that the earliest available interval is returned for processing. As intervals are popped from the min-heap, the algorithm attempts to merge them. If the currently popped interval’s start time exceeds the merged interval’s end time, a gap is identified, indicating a free period. After identifying each gap, the algorithm restarts the merging process to continue identifying additional periods of free ...
You are given schedule, a list where each element contains the working hours of one employee.
Each employee’s working hours are represented as a list of Interval objects that are already sorted and do not overlap.
Return all finite intervals with non-zero duration during which all employees are simultaneously free. The resulting list of free intervals should also be sorted in ascending order.
Note: The intervals are represented as objects, not arrays. For example,
schedule[1][1].start = 1andschedule[1][1].end = 2, whileschedule[0][0][0]is invalid. Do not include intervals with zero length (for example,[3, 3]) in the output.
Constraints:
1≤ schedule.length , schedule[i].length ≤50
0≤ interval.start < interval.end ≤108, where interval is any interval in the list of schedules.
The solution’s core revolves around merging overlapping intervals of employees and identifying the free time gaps between these merged intervals. Using a min-heap, we arrange the intervals based on when they start, sorting them according to their start times. When we pop an element from the min-heap, it guarantees that the earliest available interval is returned for processing. As intervals are popped from the min-heap, the algorithm attempts to merge them. If the currently popped interval’s start time exceeds the merged interval’s end time, a gap is identified, indicating a free period. After identifying each gap, the algorithm restarts the merging process to continue identifying additional periods of free ...