Search
⌘K
Get Premium
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 represented by a list of non-overlapping intervals sorted by start times. The function should return a list of finite, non-zero length intervals where all employees are free, also sorted in order.

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.
Visualization
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
][42][107][51][96mergedfree_times

employee free time

0 / 1

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.
Visualization
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
][42][107][51][96mergedfree_times

merge intervals

0 / 8

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.
Visualization
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
][42][107][51][96merged][51][106free_times

merge

0 / 4

Test Your Knowledge

Login to take the complexity quiz and track your progress

Complexity Analysis

Time Complexity: O(n * logn) 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

|
list of intervals [start, end]
Try these examples:
Visualization
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
][42][107][51][96mergedfree_times

employee free time

0 / 14

Your account is free and you can post anonymously if you choose.

Unlock Premium Coding Content

Interactive algorithm visualizations
Reading Progress

On This Page