LC 56 — Problem

Given an array of intervals where intervals[i] = [start_i, end_i], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

Input: intervals = 13,26,810,1518
Output: 16,810,1518
Explanation: Since intervals 13 and 26 overlap, merge them into 16.
Input: intervals = 14,45
Output: [15]
Explanation: Intervals 14 and 45 are considered overlapping.

Constraints: 1 ≤ intervals.length ≤ 10⁴ · intervals[i].length == 2 · 0 ≤ start_i ≤ end_i ≤ 10⁴

Six intervals scattered on a number line. Some overlap, some don't. Your job: find WHICH ones merge.

It looks easy — just check each pair. But how many pairs are there? With 6 intervals, that is 15 pairwise comparisons. And what if three intervals chain together? If 13 overlaps 26 and 04 overlaps 13, does 04 need to merge with 26 too — even though you never compared them directly?

Tap two bars below to check if they overlap. Find at least three overlapping pairs — and pay attention to what happens when overlaps connect. The problem might be bigger than it looks.

FIG. 1 — UNSORTED INTERVALS ON A NUMBER LINE — TAP PAIRS TO FIND OVERLAPS
02468101214161820[1,3][2,6][8,10][15,18][1,4][0,4]