2Brute2Best
🔥0

Meeting Scheduler

Medium·LeetCode #1229 Premium Pro OnlySlight Variation
LeetCode
01

Problem Statement

Two people want to meet. slots1 lists the times person 1 is free, and slots2 lists the times person 2 is free. Each slot is [start, end]. The slots of one person never overlap each other, but they are not given in any order.

Find the earliest time slot of length exactly duration when both are free. Return it as [start, start + duration]. If there is no such slot, return an empty array.

Input

Two lists of slots slots1 and slots2 (each slot is [start, end]), and an integer duration.

Output

[start, start + duration] for the earliest possible meeting, or [] if none fits.

Example 1
Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 8
Output: [60,68]

[10,50] and [0,15] share [10,15], only 5 long. [60,120] and [60,70] share [60,70], 10 long. The meeting starts at 60 and lasts 8, so [60,68].

Example 2
Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 12
Output: []

The longest shared time is [60,70], which is only 10 long.

Constraints
  • • 1 ≤ slots1.length, slots2.length ≤ 10^4
  • • slots1[i][0] < slots1[i][1], slots2[i][0] < slots2[i][1]
  • • 0 ≤ slots1[i][j], slots2[i][j] ≤ 10^9
  • • 1 ≤ duration ≤ 10^6
  • • One person's slots never overlap each other
02

Pattern Recognition Clues

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

“two people, each with free slots”
→ Two interval lists → intersect them with two pointers.
“both are free”
→ We need the overlap: [max of starts, min of ends].
“at least duration long, earliest”
→ Check hi − lo ≥ duration, and stop at the first hit because the walk goes in time order.
“slots are not sorted”
→ Sort both lists first, or the walk is meaningless.
03

Approach Complexity Ladder

Brute Force — check every pair of slotsO(M · N)O(1)

Try every pair of slots and keep the earliest overlap that is long enough

Optimal — sort both, then two pointersO(M log M + N log N)O(1)

Sort both lists, walk them together, return at the first overlap that is long enough

Brute Force Intuition

For every slot of person 1 and every slot of person 2, compute the shared part [lo, hi]. If hi − lo ≥ duration, a meeting can start at lo. Keep the smallest such lo. Since the input isn't sorted, we must look at all pairs before we know which is earliest.

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.