Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example 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] overlaps, merge them into [1,6].
Questions that can be asked:-
- Will these be sorted based on starting points
- if there is a series [[1,6],[6,7]], will this be also cojoined as [[1,7]]?
Approach:- so there can ideally be 2 cases, either an element can be overlapped or it cannot be. One approach can be, put all elements in a priorityQueue(or sort it in-place), sorted based on starting points. Iterate on that queue, check the starting point of one interval with ending point of another and voila, create and store new interval.
Time complexity:- O(nlogn) Space complexity:- O(n)
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals, (a,b)->(Integer.compare(a[0], b[0]));
LinkedList<int[]> merge = new LinkedList();
merge.add(intervals[0]);
int[] currentInterval = intervals[0];
for(int i=1;i<intervals.length;i++){
int[] interval = intervals[i];
if(currentInterval[1] > interval[0]){
currentInterval[1] = interval[1];
} else {
merge.add(interval);
currentInterval = interval;
}
}
return merge.toArray(new int[merge.size()][]);
}