{x}
blog image

Employee Free Time

Solution Explanation for Employee Free Time

This problem asks us to find the intervals of time where all employees are free, given their individual schedules. The solution involves merging all employee schedules into a unified timeline and then identifying gaps in that timeline.

Approach:

  1. Merge Intervals: We first merge all the individual employee schedules into a single sorted list of intervals. This step ensures we have a complete picture of all working hours across all employees. We can achieve this using a priority queue or by sorting all intervals then merging.

  2. Identify Free Time: Once we have the merged intervals, we scan through them to find the gaps between consecutive intervals. These gaps represent the free time periods when all employees are unavailable.

Detailed Explanation with Code (Python):

import heapq
 
class Interval:
    def __init__(self, start, end):
        self.start = start
        self.end = end
 
    def __lt__(self, other): #For heapq
        return self.start < other.start
 
 
def employeeFreeTime(schedule):
    """
    Finds the common free time for all employees.
 
    Args:
        schedule: A list of lists, where each inner list represents an employee's schedule 
                  and contains Interval objects.
 
    Returns:
        A list of Interval objects representing the common free time periods.
    """
 
    merged_intervals = []
    for employee_schedule in schedule:
        for interval in employee_schedule:
            heapq.heappush(merged_intervals, interval) #Use a min heap to efficiently merge
 
    free_time = []
    prev_end = merged_intervals[0].end #Initialize with the end of the first interval
    
    #Iterate through the sorted intervals
    for i in range(1,len(merged_intervals)):
        curr_interval = heapq.heappop(merged_intervals) #Pop the smallest interval from the heap
        if curr_interval.start > prev_end:
            free_time.append(Interval(prev_end, curr_interval.start))
        prev_end = max(prev_end, curr_interval.end)
 
    return free_time
 
 
# Example Usage
schedule = [[Interval(1, 2), Interval(5, 6)], [Interval(1, 3)], [Interval(4, 10)]]
free_times = employeeFreeTime(schedule)
 
print("Free time intervals:")
for interval in free_times:
    print(f"[{interval.start}, {interval.end}]")
 
 

Time Complexity Analysis:

  • Merging Intervals: The time complexity of merging N intervals (where N is the total number of intervals across all employees) using a heap is O(N log N) due to the heap operations. Sorting the intervals first would also be O(N log N).
  • Identifying Free Time: Scanning through the merged intervals to find gaps is a linear operation, O(N).

Therefore, the overall time complexity of the solution is dominated by the merging step and is O(N log N).

Space Complexity Analysis:

The space complexity is O(N) because in the worst case, we might need to store all intervals in the merged intervals list. The heap also occupies space proportional to the number of intervals.

Other Languages (Conceptual):

The core logic remains the same in other languages like Java, C++, or Go. The primary differences would be in the syntax and implementation of the heap data structure (PriorityQueue in Java, priority_queue in C++, container/heap in Go). The overall time and space complexity would stay the same.