Employee Free Time
DESCRIPTION (credit 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.
EXAMPLES
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.
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
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.
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
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.
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
Complexity Analysis
Time Complexity: O(n * logn) where n is the number of intervals due to the sorting step. Space Complexity: O(n) for the free_times output array.
Solution
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
Loading comments...