LeetCode pattern lab · temporal boundaries

Sort the moments.Resolve the overlap.

Lay intervals onto one timeline, expose the active boundary, and turn a tangled schedule into a single left-to-right decision.

Mission controls

Configure the temporal map

Sort by start · consolidate

Current mission

Merge Intervals

Combine every touching or overlapping closed interval into disjoint regions.

Medium
Steps10
Checkpoints5
Intervals4

Playback console

Control the sweep

1 / 10

Choose a mission, inspect the sort key, then launch the sweep.

Temporal cartography

Choose the sweep order

Intervals may arrive in any order. A left-to-right sweep needs their start boundaries ordered first.

Intervals4
Output0
Sortstart ↑

Sorted source · by start

Each band occupies its actual numeric span.

0 → 18
[1, 3]
[2, 6]
[8, 10]
[15, 18]

Committed output

HistoryNo intervals absorbed or removed yet.

Live decision inspector

Which boundary should define the sort order?

Unordered intervals
sort by start ascending

Next action

Arrange the timeline

Once starts are ordered, only the last merged interval can overlap the next candidate.

Invariant

Every unseen interval starts no earlier than the current candidate.

Execution trace

Code in motion

def merge(intervals):
intervals.sort(key=lambda x: x[0])
output = [intervals[0]]
for start, end in intervals[1:]:
if start <= output[-1][1]:
output[-1][1] = max(output[-1][1], end)
else:
output.append([start, end])
return output

Why does this work?

Proof laboratory

Predict the interval classification first, then reveal the exchange or sweep invariant.

Local mastery

Your pattern record

Merge Intervals0 runs0%
Insert Interval0 runs0%
Non-overlapping Intervals0 runs0%

Completion and best checkpoint accuracy remain in this browser.

Sweep in progress

The boundary is still moving.

Complete the mission to record it. Prediction mastery requires at least 70% accuracy.

Recognize the pattern

When should you turn data into a timeline?

Look for ranges, bookings, meetings, coverage windows, or start/end events. The decisive move is usually sorting one boundary so a local comparison can safely finalize everything behind the sweep.

Are values really start/end boundaries?
Does touching count as overlap?
Should I sort by start or by finish?
What part of the output is still mutable?

Sweep recipe

  1. 01Define overlap semantics at equal endpoints.
  2. 02Choose the sort key that makes the greedy state local.
  3. 03Track the only interval or boundary still allowed to change.
  4. 04Commit everything behind the sweep permanently.