10. Merge Intervals

You are given an array intervals where intervals[i] = [startᵢ, endᵢ] represents the inclusive interval startᵢ ≤ x ≤ endᵢ.

Your task is to merge every pair of intervals that overlap and return all the non-overlapping intervals that completely cover the same ranges as the original array.

Two intervals overlap if they share at least one common point (i.e. start₂ ≤ end₁ and start₁ ≤ end₂).

Return the merged intervals in any order.

Example 1:

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

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

Explanation: [1,3] and [2,6] overlap --> merge to [1,6].

Example 2:

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

Output: [[1,5]]

Explanation: Because the end of [1,4] equals the start of [4,5], they are considered overlapping.

Now Your Turn!

Pick the correct output for the given input

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

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 ≤ intervals.length ≤ 10⁴
  • intervals[i].length == 2
  • 0 ≤ startᵢ ≤ endᵢ ≤ 10⁴

Fun Facts

0
class Solution {
public:
vector<vector<int>> mergeIntervals(vector<vector<int>>& intervals) {
// Your code goes here
}
};
Test Case

Input:

Intervals