Skip to content

Latest commit

 

History

History
38 lines (31 loc) · 1.48 KB

MergeIntervals.md

File metadata and controls

38 lines (31 loc) · 1.48 KB

The question

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:-

  1. Will these be sorted based on starting points
  2. 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()][]);
}