Explorer
Data Structures & Algorithms

Merge all overlapping intervals and return the simplified non-overlapping intervals.

Problem Statement

Given an array of `intervals` where `intervals[i] = [start_i, end_i]`, merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

Examples

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

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

Explanation: Since intervals [1, 3] and [2, 6] overlap, merge them into [1, 6].

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

Output: [[1, 5]]

Explanation: Combining the input according to Merge Overlapping Subintervals logic yields [[1, 5]].

Complexity

Time Complexity: O(N log N)

Space Complexity: O(N)

Hints

šŸ’” Hint 1: Sort the intervals by their start times: intervals.sort((a, b) => a[0] - b[0]). šŸ’” Hint 2: Iterate through sorted intervals. If current interval starts before the previous one ends, they overlap. šŸ’” Hint 3: Merge overlapping intervals by setting last[1] = Math.max(last[1], current[1]). Otherwise push as a new interval.

Editorial & Approach

Problem Overview & Intuition

By sorting intervals by their start coordinate, any overlapping intervals will appear consecutively. Comparing the current interval start with the previous interval end resolves all merges in a single linear pass.

Step-by-Step Approach

  1. Sort intervals by starting value a[0] - b[0].
  2. Initialize result = [intervals[0]].
  3. Iterate from i = 1 to intervals.length - 1.
  4. If intervals[i][0] <= last[1]: merge by last[1] = Math.max(last[1], intervals[i][1]).
  5. Otherwise, push intervals[i] to result.
  6. Return result.

Optimal Implementation (JavaScript)

function mergeIntervals(intervals) {
  if (!intervals.length) return [];
  intervals.sort((a, b) => a[0] - b[0]);
  const result = [intervals[0]];
  for (let i = 1; i < intervals.length; i++) {
    const current = intervals[i];
    const last = result[result.length - 1];
    if (current[0] <= last[1]) {
      last[1] = Math.max(last[1], current[1]);
    } else {
      result.push(current);
    }
  }
  return result;
}

Complexity Analysis

Time Complexity O(N log N) — sorting takes O(N log N), merging takes O(N).
Space Complexity O(N) — storage for sorted/output intervals.

Edge Cases & Corner Traps Handled

  • Single interval: returns [interval].
  • Adjacent touching intervals [1, 4] and [4, 5]: merge to [1, 5].
  • One interval completely containing another [1, 10] and [2, 6].

Merge Overlapping Subintervals

Hard
Given an array of `intervals` where `intervals[i] = [start_i, end_i]`, merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example Scenarios
1Example 1
Input: intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]
Output: [[1, 6], [8, 10], [15, 18]]
Explanation:

Since intervals [1, 3] and [2, 6] overlap, merge them into [1, 6].

2Example 2
Input: intervals = [[1, 4], [4, 5]]
Output: [[1, 5]]
Explanation:

Combining the input according to Merge Overlapping Subintervals logic yields [[1, 5]].

Editor
Loading Editor...
intervals =
[[1, 3], [2, 6], [8, 10], [15, 18]]
Output:Click "Run" above to execute and verify your code here.
[[1,6],[8,10],[15,18]]