leet++Merge Intervals
MediumSortingIntervals

Merge Intervals

Given a collection of numeric ranges, combine ranges that share any values and return the resulting non-overlapping ranges in ascending order.

Examples

Input: ranges = [[2, 6], [4, 9], [13, 15]]

Output: [[2, 9], [13, 15]]

The first two ranges overlap and form one range.

Input: ranges = [[5, 7], [1, 3]]

Output: [[1, 3], [5, 7]]

The ranges are separate and are returned in order.

Constraints & goals

  • Each range has a start no greater than its end.
  • Input ranges may be in any order.
  • Touching endpoints count as overlapping.

Camera feed

Preview
Camera starts when the session begins
Read-only

Transcribed whiteboard text will appear here.