Skip to content
RED SAGSkills
medium30 ptsintervalssorting

Combine Overlapping Ranges

You are given a list of closed ranges ranges, where ranges[i] = [start, end]. Merge every set of ranges that overlap or touch (share an endpoint), and return the merged ranges sorted by start.

Example 1

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

Example 2

Input: ranges = [[1, 4], [4, 5]]
Output: [[1, 5]]
Explanation: Touching ranges merge.

Constraints

  • 1 <= ranges.length <= 10^4
  • 0 <= start <= end <= 10^9
Hint 1

Sort by start first.

Hint 2

Each range either extends the last merged range or starts a new one.

Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Game Developer, Automation Test Engineer, Site Reliability Engineer

Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.