Algorithms Playbook
← All problems
MediumIntervals →Similar to: Merge Intervals

Merge the overlapping ranges

Problem

Given a list of intervals [start, end], merge every pair that overlaps (including touching ones) and return the resulting non-overlapping intervals, sorted by start.

Examples

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

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

Input: [[1,4],[4,5]]

Output: [[1,5]]

Hints

    Try it first. Every hint reveals a bit more.

    Solution