CPKit
v1.6.1

Merge Intervals

GreedyMedium

Sort by start times and merge overlapping ranges greedily.

Alt+Shift+M

Configuration Panel

Intervals Merging Visual Comparison

Original Input Intervals (stacked):
Timeline is empty.
Merged Output Intervals (aligned):
Timeline is empty.

Time Complexity

O(N log N) sorting + O(N) merge

Space Complexity

O(N) mapping intervals storage
Conceptual Overview

The Merge Intervals problem merges all overlapping intervals from a given collection and returns an array of the non-overlapping intervals.

Greedy Choice Property

Sort intervals by start times ascending. Maintain a list of merged intervals; if the current interval overlaps with the last merged one, merge them by extending the end boundary.

Optimal Substructure

Sorting by start times ensures that once we process interval i and it does not overlap with the current merged range, no subsequent interval can overlap with it either.

Proof Idea / Correction

Proof by induction: since intervals are sorted by start time, interval i has start >= previous start. If start > last_end, all subsequent intervals also have start > last_end, guaranteeing separation.

Source: CP-Algorithms greedy reference