Sorting + Intervals

Use for sweep/merge, greedy choices, custom ordering.

What this pattern is

Sort by start (or end) and then sweep/merge in a single pass. Used for union/merge, counting rooms, and selecting non-overlapping intervals.

Exercise: mergeIntervals

Task: Given intervals = [[s1,e1],[s2,e2],...], merge all overlapping intervals and return the result.
Example: [[1,3],[2,6],[8,10],[15,18]] → [[1,6],[8,10],[15,18]]
Edge cases: nested intervals, identical boundaries, unsorted input.

Your Solution