56. Merge Intervals

56. Merge Intervals
https://www.youtube.com/watch?v=6tLHjei-f0I
这个就是 sweep line 

Time nlogn sorting and o(n) for merge 
So nlogn in total

Space o(n) if no overlapping intervals 
https://github.com/tongzhang1994/Facebook-Interview-Coding/blob/master/56.%20Merge%20Intervals.java


注意:1.别忘先sort 2.中间用if-else 3.loop后别忘add最后一个interval


  // sort the intervals by starting time first 
      
      // take the first interval , and compare with the second 
      // interval 
      
      // and check if the second interval  starts before the 
      // first interval ends, if yes, take the max of their end as the end 
      // of the first interval. 
      
      //if no overlap, then add the first interval into the 
      // result, and move onto the second interval inorder to compare with the rest 
      
      



/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
class Solution {
    public List<Interval> merge(List<Interval> intervals) {
      if(intervals.size() <= 1){
        return intervals;
      }
      
      // Collections.sort(), not Arrays.sort()
      Collections.sort(intervals, new Comparator<Interval>(){
        @Override
        public int compare(Interval i1, Interval i2){
          if(i1.start < i2.start){
            return -1;
          }else if( i1.start > i2.start){
            return 1;
          }else{
            return 0;
          }
      });
        
      int start = intervals.get(0).start;
      int end = intervals.get(0).end;
        
      List<Interval> result = new ArrayList<>();
        
      for(Interval interval : intervals){
        if(interval.start <= end){
          end = Math.max(interval.end, end);
        }else{
          result.add(new Interval(start, end));
          start = interval.start;
          end = interval.end;
        }
      }
      result.add(new Interval(start, end));
      return result;
      }
} 
     

 

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
class Solution {
    public List<Interval> merge(List<Interval> intervals) {
      if(intervals.size() <= 1){
        return intervals;
      }
      List<Interval> result = new ArrayList<>();
      // sort the intervals by starting time first 
      
      // take the first interval , and compare with the second 
      // interval 
      
      // and check if the second interval  starts before the 
      // first interval ends, if yes, take the max of their end as the end 
      // of the first interval. 
      
      //if no overlap, then add the first interval into the 
      // result, and move onto the second interval inorder to compare with the rest 
      
 
      intervals.sort((i1, i2) -> Integer.compare(i1.start, i2.start));
    
      int start = intervals.get(0).start;
      int end = intervals.get(0).end;
      
      for(Interval interval : intervals) {
        if(interval.start <= end){
          // merge
          end = Math.max(end, interval.end);
        }else{
          result.add(new Interval(start, end));
          start = interval.start;
          end = interval.end;
        }
      }
      
      result.add(new Interval(start, end));
      return result;
    }
}

 

  1. merge intervals leetcode 88 merge interval的变体, 问了下时间复杂度,求interval所覆盖区间的总长度。Follow up: nlogn的排序算法有哪些。哪些排序算法是稳定的。解释为什么quick sort不稳定。我没想到没问这么基础的问题,答得一般般啦。小姐姐让我写个merge,保证merge后的结果是稳定的(因为我当时“嘴欠”,说了merge sort可能不稳定)。所以大家刷题的同时,也注意下基础 , //变种 写一个 API totalTime(int[] newInterval),返回现有的所有 interval(start, end) 所占的总时间。Overlap 的不要重复计。tongzhang1994

Given a collection of intervals, merge all overlapping intervals.

Example 1:

Input: [[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].

Example 2:

Input: [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considerred overlapping.

 

posted on 2018-08-10 14:42  猪猪&#128055;  阅读(147)  评论(0)    收藏  举报

导航