Merge Intervals

Medium

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.

Constraints

  • 1 <= intervals.length <= 10^4
  • intervals[i].length == 2
  • 0 <= start_i <= end_i <= 10^4

Example

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

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

Edge Cases

  • If the interval list is empty, return an empty list.
  • If there is only one interval, return it as it is.
  • If intervals touch at the boundary (e.g., [1,3] and [3,5]), treat them as overlapping if merging on touching edges is allowed.
  • If no intervals overlap, return the original list unchanged.