zoukankan      html  css  js  c++  java
  • [LintCode] Merge Intervals

    Given a collection of intervals, merge all overlapping intervals.

    Example

    Given intervals => merged intervals:

    [                     [
      [1, 3],               [1, 6],
      [2, 6],      =>       [8, 10],
      [8, 10],              [15, 18]
      [15, 18]            ]
    ]
    
    Challenge 

    O(n log n) time and O(1) extra space.

    Solution 1.   Brute force, O(n^2) runtime

    An obvious solution is to iterate through the input interval list. For each interval, check if if there is overlap between itself and other intervals.

    Solution 2.   Sorting, O(n * log n) runtime

    1. sort the interval lists in increasing order of the start.

    2.  iterate through the sorted list,  and do the following.

      a. if the previous and current interval overlap (curr.start <= prev.end), merge these two by doing prev.end = Math.max(prev.end, curr.end);

      b. if they don't overlap, then add the previous interval to the final result.

     1 /**
     2  * Definition of Interval:
     3  * public class Interval {
     4  *     int start, end;
     5  *     Interval(int start, int end) {
     6  *         this.start = start;
     7  *         this.end = end;
     8  *     }
     9  */
    10 
    11 class Solution {
    12     /**
    13      * @param intervals, a collection of intervals
    14      * @return: A new sorted interval list.
    15      */
    16     private Comparator<Interval> intervalComp = new Comparator<Interval>(){
    17         public int compare(Interval i1, Interval i2){
    18             return i1.start - i2.start;
    19         }    
    20     };
    21     public List<Interval> merge(List<Interval> intervals) {
    22         if(intervals == null || intervals.size() <= 1){
    23             return intervals;
    24         }
    25         Collections.sort(intervals, intervalComp);
    26         ArrayList<Interval> result = new ArrayList<Interval>();
    27         Interval prev = intervals.get(0);
    28         for(int i = 1; i < intervals.size(); i++){
    29             Interval curr = intervals.get(i);
    30             if(curr.start <= prev.end){
    31                 prev.end = Math.max(prev.end, curr.end);
    32             }
    33             else{
    34                 result.add(prev);
    35                 prev = curr;
    36             }
    37         }
    38         result.add(prev);
    39         return result;
    40     }
    41 }

    Similarly we can sort the list by their end values in decreasing order, then do the merge on start values.  This alternative solution returns the merged list in reversed order.

     1 class Solution {
     2     private Comparator<Interval> intervalComp = new Comparator<Interval>(){
     3         public int compare(Interval i1, Interval i2){
     4             return i2.end - i1.end;
     5         }    
     6     };
     7     public List<Interval> merge(List<Interval> intervals) {
     8         if(intervals == null || intervals.size() <= 1){
     9             return intervals;
    10         }
    11         Collections.sort(intervals, intervalComp);
    12         ArrayList<Interval> result = new ArrayList<Interval>();
    13         Interval prev = intervals.get(0);
    14         for(int i = 1; i < intervals.size(); i++){
    15             Interval curr = intervals.get(i);
    16             if(curr.end >= prev.start){
    17                 prev.start = Math.min(prev.start, curr.start);
    18             }
    19             else{
    20                 result.add(prev);
    21                 prev = curr;
    22             }
    23         }
    24         result.add(prev);
    25         return result;
    26     }
    27 }

    Related Problems

    Insert Interval

    Number of Airplanes in the Sky

  • 相关阅读:
    mybatis源码追踪2——将结果集映射为map
    Mybatis的cache
    mybatis拦截器
    mybatis中单个参数的引用
    mybatis源码追踪1——Mapper方法用法解析
    win8 下 intellij idea 13 中文输入覆盖的问题
    firebug中html显示为灰色的原因总结
    extjs4.0以上添加多行工具栏的方法
    去除eclipse的validating
    An interview question from MicroStrategy
  • 原文地址:https://www.cnblogs.com/lz87/p/7097045.html
Copyright © 2011-2022 走看看