Intervals
Employee Free Time
hard
DESCRIPTION (inspired by Leetcode.com)
Write a function to find the common free time for all employees from a list called schedule. Each employee's schedule is a list of non-overlapping intervals, sorted by start time, representing the times that employee is busy (their meetings). The function should return the finite, non-zero length intervals during which every employee is free, sorted in order. Free time is the gaps that sit between busy periods once all the schedules are combined. There's no fixed workday here, so time before everyone's first meeting or after everyone's last doesn't count — that's what "finite" means.
Input:
schedule = [[[2,4],[7,10]],[[1,5]],[[6,9]]]
Output:
[[5,6]]
Explanation: The three employees collectively have only one common free time interval, which is from 5 to 6.
Explanation
This problems builds upon the concept of merging intervals. We can solve this problem by first merging all the employee meeting intervals into a single list. The free times are then the gaps between those merged intervals.
Important Note on Boundaries : In this problem, we only consider the gaps between busy intervals as free time. We do not consider:
- Time before the earliest busy interval (e.g., if the first meeting starts at 9:00 AM, we don't count 8:00-9:00 AM as "free time")
- Time after the latest busy interval (e.g., if the last meeting ends at 5:00 PM, we don't count 5:00-6:00 PM as "free time")
This is because the problem asks for common free time when all employees are available, and we're only given their scheduled busy intervals within a certain working timeframe.
Phase 1
We first want to flatten the list of intervals into a single list, and then sorting them by their start time to make the merge process easier.
def employeeFreeTime(schedule):
flattened = [i for employee in schedule for i in employee]
intervals = sorted(flattened, key=lambda x: x[0])
merged = []
for interval in intervals:
if not merged or merged[-1][1] < interval[0]:
merged.append(interval)
else:
merged[-1][1] = max(merged[-1][1], interval[1])
free_times = []
for i in range(1, len(merged)):
start = merged[i-1][1]
end = merged[i][0]
free_times.append([start, end])
return free_times
employee free time
Phase 2
Next, we want to merge all the intervals into a single list. We can do this by iterating through the list of intervals and comparing the end time of the current interval with the start time of the next interval. If the end time of the current interval is greater than or equal to the start time of the next interval, we merge the two intervals. Otherwise, we add the current interval to the merged list.
merge intervals
Phase 3
In this phase, we return the employee free times by finding the gaps between the merged intervals. We can do this by iterating through the merged intervals, and creating a new interval from the end time of the current interval and the start time of the next interval.
merge
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(n log n) where n is the number of intervals. The time complexity is dominated by the sorting step.
Space Complexity: O(n) where n is the number of intervals. We need space for the free_times output array.
Solution
employee free time