Data Structures19 sections · 729 units
Open in Course

Problem - Merge Intervals

Classic interval problem

Given an array of intervals, merge all overlapping intervals and return the non-overlapping result.

Example: [[1,3],[2,6],[8,10],[15,18]][[1,3], [2,6], [8,10], [15,18]] becomes [[1,6],[8,10],[15,18]][[1,6], [8,10], [15,18]] because [1,3][1,3] and [2,6][2,6] overlap. You're solving a foundational interval problem.

While you can solve it with sorting alone, understanding it prepares you for harder interval tree problems. Constraints: up to 10410^4 intervals, values up to 10410^4.