WorkspaceProblem lab
ALGORITHM VISUALIZER

Merge Intervals

Opening the interactive workspace…

Understanding Merge Intervals

Merge overlapping closed intervals and return the disjoint intervals sorted by starting value. Touching endpoints overlap: [1, 4] and [4, 5] merge.

How it works

After sorting by start, a new interval can only overlap the last merged interval.

Sort intervals, start a new group when the next start exceeds the current end, and otherwise extend the current end.

Try an example

Input: intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]

Output: [[1, 6], [8, 10], [15, 18]]

The first two intervals overlap and combine into [1, 6].

Constraints

  • At most 32 intervals [start, end] with start ≤ end.
  • Endpoints are integers between −1,000 and 1,000.
  • Return intervals in ascending start order.

Use the simulator

Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.

Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.

Related concepts and complexity