Merge Intervals

Greedy & Intervals, problem 5 of 8

Merge Intervals

Medium

LC #56

intervalssorting

Not attempted yet

Given a list of intervals where each is [start, end], merge every group of overlapping intervals and return the resulting non-overlapping intervals. Intervals that touch ([1,4] and [4,5]) overlap.

Return the intervals in any order.

Example 1

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

[1,3] and [2,6] overlap, so they become [1,6].

Example 2

Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]

Example 3

Input: intervals = [[4,7],[1,4]]
Output: [[1,7]]

The input is not necessarily sorted.

Constraints

  • 1 <= intervals.length <= 10^5
  • 0 <= start <= end <= 10^5

Python

Loading draft…

Test results

8 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 5 hidden

Run examples, then submit all tests.