Explanation
Merge Intervals is a Medium interview problem tagged [Array, Sorting, Interval]. The winning approach is typically **Intervals**.
#56
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example 1
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
[1,3] and [2,6] overlap, so merge to [1,6].
Example 2
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Intervals that touch are considered overlapping.
Merge Intervals is a Medium interview problem tagged [Array, Sorting, Interval]. The winning approach is typically **Intervals**.
Ordering makes overlaps adjacent and trivial to detect.
Sort intervals by start (or end), then merge or sweep with a counter/heap.
Pattern-level skeleton only — not this problem’s exact answer. Adapt it to the statement.
Time: O(n log n) or O(n)
Space: O(n)
Before coding, restate the problem, name the pattern (Intervals), and write the invariant or DP state in one sentence.
Got it?
Mark #56 Merge Intervals as understood when the pattern clicks.