We are given a list of employees, each with a list of non-overlapping interval representing their working time. Each employee's working time list is sorted.
Return the list of finite intervals representing common, positive-length free time for all employees.
You may return the result in any order.
Example 1:
Input: schedule = [[[1,2],[5,6]],[[1,3]],[[4,10]]]
Output: [[3,4]]
Explanation: There are a total of three employees, and all common
free time intervals would be [[3,4]].
Example 2:
Input: schedule = [[[1,3],[6,7]],[[2,4]],[[2,5],[9,12]]]
Output: [[5,6],[7,9]]
Constraints:
1 <= schedule.length , schedule[i].length <= 50
0 <= schedule[i].start < schedule[i].end <= 108
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding employee free time is like checking every single possible time slot to see if everyone is busy. We need to exhaustively compare each employee's work schedule with every other employee's work schedule to find the gaps where no one is working.
Here's how the algorithm would work step-by-step:
def employee_free_time_brute_force(schedules):
all_intervals = []
for employee_schedule in schedules:
all_intervals.extend(employee_schedule)
# Sort intervals to easily find overlaps and gaps
all_intervals.sort(key=lambda interval: interval[0])
free_intervals = []
if not all_intervals:
return free_intervals
current_time = 0
end_of_previous_interval = all_intervals[0][0]
for interval in all_intervals:
start_time = interval[0]
end_time = interval[1]
# Check for gaps between previous interval and current interval
if start_time > end_of_previous_interval:
free_intervals.append([end_of_previous_interval, start_time])
#Update the end time of previous interval
end_of_previous_interval = max(end_of_previous_interval, end_time)
return free_intervals
The most efficient approach to finding employee free time involves merging all employee schedules into one sorted timeline. Then, we simply identify the gaps in this unified timeline to find the free time slots. This avoids pairwise comparisons between employees, significantly reducing the work needed.
Here's how the algorithm would work step-by-step:
class Interval:
def __init__(self, start, end):
self.start = start
self.end = end
def employee_free_time(schedules):
merged_schedule = []
for employee_schedule in schedules:
merged_schedule.extend(employee_schedule)
# Sort the intervals by start time.
merged_schedule.sort(key=lambda interval: interval.start)
available_time_slots = []
latest_busy_time = merged_schedule[0].end
for interval in merged_schedule[1:]:
# If the current interval starts after the latest busy time,
# there's a free time slot.
if interval.start > latest_busy_time:
available_time_slots.append(Interval(latest_busy_time, interval.start))
# Update the latest busy time if the current interval extends it.
latest_busy_time = max(latest_busy_time, interval.end)
return available_time_slots
Case | How to Handle |
---|---|
Input schedule is null or empty | Return an empty list of intervals because there are no employee schedules to process. |
Individual employee schedule is null or empty | Treat an empty employee schedule as if the employee is always available, effectively ignoring it for free time calculation. |
Overlapping intervals within a single employee's schedule | Merge overlapping intervals for each employee's schedule before considering free time. |
Large number of employees with many intervals each | Ensure the chosen algorithm scales efficiently, considering space and time complexity for merging all intervals. |
Employee schedules contain adjacent intervals (e.g., [1,2],[2,3]) | Ensure adjacent intervals are not mistakenly identified as free time; handle them correctly during the merging process. |
All employees have completely overlapping schedules (no free time) | Return an empty list indicating there is no free time available for all employees. |
Intervals with very large start and end times leading to potential integer overflow | Use data types that can accommodate large time values, such as long, to prevent overflow issues. |
Intervals are not sorted within each employee's schedule | Sort each employee's schedule by start time before processing to ensure proper interval merging and free time identification. |