LeetCode 759: Employee Free Time Solution

Master LeetCode problem 759 (Employee Free Time), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

759. Employee Free Time

Problem Explanation

Explanation

To find the free time between the schedules of multiple employees, we can first merge all the intervals into a single list and then sort them based on their start times. We then iterate through the sorted list to find the gaps between the intervals, which represent the free time.

Algorithm:

  1. Merge all the intervals into a single list.
  2. Sort the merged list based on the start times.
  3. Iterate through the sorted list to find the free time gaps between intervals.

Time Complexity

The time complexity of this approach is O(n log n), where n is the total number of intervals.

Space Complexity

The space complexity is O(n) to store the merged intervals.

Solution Code

import java.util.*;

class Solution {
    public List<Interval> employeeFreeTime(List<List<Interval>> schedule) {
        List<Interval> mergedIntervals = new ArrayList<>();
        for (List<Interval> employeeSchedule : schedule) {
            mergedIntervals.addAll(employeeSchedule);
        }
        
        Collections.sort(mergedIntervals, (a, b) -> a.start - b.start);
        
        List<Interval> result = new ArrayList<>();
        Interval prev = mergedIntervals.get(0);
        for (Interval curr : mergedIntervals) {
            if (prev.end < curr.start) {
                result.add(new Interval(prev.end, curr.start));
                prev = curr;
            } else {
                prev = prev.end < curr.end ? curr : prev;
            }
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 759 (Employee Free Time)?

This page provides optimized solutions for LeetCode problem 759 (Employee Free Time) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 759 (Employee Free Time)?

The time complexity for LeetCode 759 (Employee Free Time) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 759 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 759 in Java, C++, or Python.

Back to LeetCode Solutions