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

  • 相关阅读:
    关于Netty4.x中文教程系列更新进度的说明和道歉
    Netty4.x中文教程系列(四) ChannelHandler
    Netty4.x中文教程系列(三) Hello World !详解
    Netty4.x中文教程系列(二) Hello World !
    Netty4.x中文教程系列(一) 目录及概述
    【推荐】HTML5 UI框架 推荐
    【转载】【JQuery学习】jQuery插件开发
    前端与后端的数据交互(jquery ajax+python flask)
    【JS教程32】ajax
    【JS教程31】json
  • 原文地址:https://www.cnblogs.com/lz87/p/7097045.html
Copyright © 2011-2022 走看看