2Brute2Best
🔥0

Employee Free Time

Hard·LeetCode #759 Premium Pro OnlyPattern Transfer
LeetCode
01

Problem Statement

You get the work schedule of k employees. schedule[e] is the list of times employee e is busy. Each employee's list is sorted and its intervals don't overlap.

Return every stretch of time when all employees are free at once, as a sorted list of intervals. Only return gaps of positive, finite length: skip the endless time before everyone's first interval and after everyone's last one, and skip a "gap" like [5,5] where one interval ends exactly when another starts.

About the signature: on LeetCode the input is vector<vector<Interval>>, where Interval is a small class LeetCode gives you with two fields, start and end. So schedule[e][i].start is when employee e's i-th busy interval begins. The answer is a vector<Interval>. Our code shows Interval as a plain struct only so it is readable on its own.

Input

schedule: a list of k lists. schedule[e] holds employee e's busy intervals, sorted by start.

Output

The common free intervals, sorted by start. It can be empty.

Example 1
Input: schedule = [[[1,2],[5,6]], [[1,3]], [[4,10]]]
Output: [[3,4]]

Together, people are busy during [1,3] and [4,10]. The only finite gap is [3,4]. Time before 1 and after 10 is endless, so it is left out.

Example 2
Input: schedule = [[[1,3],[6,7]], [[2,4]], [[2,5],[9,12]]]
Output: [[5,6],[7,9]]

Busy time joined together: [1,5], [6,7], [9,12]. The gaps between them are [5,6] and [7,9].

Constraints
  • • 1 ≤ schedule.length ≤ 50
  • • 1 ≤ schedule[i].length ≤ 50
  • • 0 ≤ schedule[i][j].start < schedule[i][j].end ≤ 10^8
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“all employees are free”
→ Nobody is busy → only the union of busy times matters, not who is busy.
“each employee's list is sorted”
→ k sorted lists → k-way merge with a min-heap instead of a full sort.
“finite, positive-length intervals”
→ Skip the endless time before the first and after the last interval, and skip start == lastEnd.
03

Approach Complexity Ladder

Brute Force — check every possible gap startO(N²)O(N)

A gap can only start where some interval ends; test each end against all intervals

Better — flatten, sort, merge, report gapsO(N log N)O(N)

Sort all intervals by start; walk with lastEnd and report every gap

Optimal — k-way merge with a min-heapO(N log k)O(k)

Merge the k sorted lists with a min-heap; report a gap whenever the next start passes lastEnd

Brute Force Intuition

Put every busy interval into one list. A free gap can only begin at some interval's end. For each distinct end e, check all intervals: if nobody is busy at e (no interval has start ≤ e < end), the gap runs from e to the next start after e. If nothing starts after e, the gap is endless, so skip it.

Recognition rule for this sub-pattern:

Two (or k) interval lists in time order — sort first if needed. Output their overlap, then advance whichever ends first; with k lists, use a min-heap.
Remember this · Intervals
Trigger
Input is [start, end] pairs; overlap, merge, insert, meeting rooms, free time, minimum removals.
Think
After sorting, do I only ever need to compare against the most recent interval?
Key question
Sort by start or by end — which one makes the greedy choice obvious?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Intervals translate into production architectures and low-level memory mechanics.