The problem. You are given an array of intervals, where intervals[i] = [start, end]. Merge all overlapping intervals and return the non-overlapping intervals that cover exactly the same ranges.
Input: [[1,3], [2,6], [8,10], [15,18]]
Output: [[1,6], [8,10], [15,18]] [1,3] and [2,6] overlap, so they become [1,6]
Input: [[1,4], [4,5]]
Output: [[1,5]] touching intervals count as overlappingTwo intervals overlap when one starts before (or exactly when) the other ends. Merging them gives an interval from the earlier start to the later end. Think of meetings in a calendar: back-to-back or overlapping meetings merge into one busy block.
The input isn't sorted — the classic test case lists [15, 18] before [2, 6] — and that's what makes it feel hard at first.
Free account
Sign up to read the rest of this lesson: 5 more sections, 3 drawings, a dry-run simulator and code in JavaScript, Python, Java and C++.
Still to come
Was this helpful?