Problem
Merge Intervals
Medium- arrays
- sorting
Each interval is a pair [start, end], and intervals is in no particular order. Whenever two intervals overlap, replace them with one interval that covers both. Keep going until no two intervals overlap, and return the result sorted by start.
Intervals that share only an endpoint, such as [1, 4] and [4, 5], count as overlapping.
- Input
intervals = [[5, 9], [1, 3], [2, 4], [12, 15]]- Output
[[1, 4], [5, 9], [12, 15]]- Explanation
[1, 3]and[2, 4]overlap and become[1, 4]. The other two intervals overlap nothing. The input is not sorted, but the result is.
- Input
intervals = [[3, 6], [6, 8]]- Output
[[3, 8]]- Explanation
the intervals meet at 6, and touching counts as overlapping.
- Input
intervals = [[1, 10], [3, 5], [12, 20], [11, 13]]- Output
[[1, 10], [11, 20]]- Explanation
[3, 5]lies inside[1, 10], so it changes nothing.[11, 13]and[12, 20]overlap and become[11, 20]. 10 and 11 do not touch, so the two groups stay apart.
Constraints:
1 <= intervals.length <= 10^5start <= endfor every interval-10^9 <= start, end <= 10^9
Tab indents. Press Esc, then Tab to leave the editor.
Run your code to see every test here. Nothing is submitted or recorded.