InferenceAI

Enter at least two characters to search.

Start free

Merge Overlapping Intervals

mediumGreedy & Intervals25 min25 XP

Merge all overlapping intervals and return the result sorted by start. Intervals that touch (such as [1, 4] and [4, 5]) also merge.

Examples

EXAMPLE WALKTHROUGHone merge

A guided view of the published example, not a trace of your code.

INPUT[[[1,3],[2,6],[8,10],[15,18]]]
TASKmerge_overlapping_intervals()
EXPECTED OUTPUT[[1,6],[8,10],[15,18]]

Start with the input values shown in this example.

Example 1 of 3

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

Output: [[1,6],[8,10],[15,18]]

Explanation: one merge

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

Output: [[1,5]]

Explanation: touching

Input: intervals = []

Output: []

Explanation: empty

Hint 1

Sort by start; extend the last merged interval while the next one starts before it ends.

Topics

DSAGreedy & IntervalsPython