15% off all plans & roadmaps Use code
See plansLeetCode 56 · Solution & editorial
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Merge Intervals is a Greedy Patterns problem, in the Interval Merging/Scheduling subpattern. The pattern covers interval scheduling, jump games, stock trading, and task scheduling
Master interval problems including merge intervals, insert interval, and non-overlapping intervals.
The reference solution runs in O(N log N) time and O(N) space.
Merge Intervals has been reported in interviews at Facebook, JPMorgan, Amazon, Bloomberg, Apple, Google, Microsoft, Adobe.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Merge Intervals is a Greedy Patterns problem, in the Interval Merging/Scheduling subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.