Merge Intervals
Medium Intervals
intervals is a list of intervals, each [start, end] with start ≤ end, in no particular order. An interval holds every point from its start to its end, both ends included.
Two intervals overlap when they share at least one point, even a single end point. Join every group of intervals that overlap, directly or through others in the group, into one interval from the group's smallest start to its largest end.
Return the joined intervals, which no longer overlap, in any order.
Example 1
[3, 5], [5, 9] and [9, 11] each meet the next at an end point, so the three join into [3, 11]. [16, 17] lies inside [14, 20], which adds nothing, and the single point [24, 24] touches nothing.
Example 2
Constraints
1 ≤ intervals.length ≤ 10⁵intervals[i] = [start, end]0 ≤ start ≤ end ≤ 10⁹
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundWatch the explanation
More Intervals
- Insert IntervalAI round · Medium
- Non-overlapping IntervalsAI round · Medium
- Meeting Rooms IIMedium
- The Skyline ProblemAI round · Hard
- Interval List IntersectionsMedium