You are given a collection of intervals, intervals, where each intervals[i] is an array [starti, endi] representing an interval.
Your task is to merge all overlapping intervals within this collection.
Note that:
[starti, endi] guarantees that starti <= endi.Return a new array containing only the non-overlapping intervals that collectively cover all intervals from the initial input.
[ [1, 3], [2, 5] ]
[ [1, 5] ]
Intervals [1,3] and [2,5] overlap, so they are merged into [1,5].
[ [1, 3], [5, 7] ]
[ [1, 3], [5, 7] ]
Intervals [1,3] and [5,7] do not overlap, so they remain separate.
[ [1, 3], [3, 5], [7, 9] ]
[ [1, 5], [7, 9] ]
Intervals [1,3] and [3,5] overlap at the endpoint, so they are merged into [1,5]. Interval [7,9] remains separate.
[ [1, 3], [2, 5] ]
[ [1, 5] ]