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

  • 相关阅读:
    ElasticSearch基础之批量操作(mget+mbulk)
    ElasticSearch基础+文档CRUD操作
    Flask_restful 插件实战笔记——基本概念与使用
    Django rest framework之序列化小结
    我所理解的Restful API最佳实践
    ElasticSearch入门及核心概念介绍
    生产环境下Flask项目目录构建
    利用linq的Take Skip方法解决 集合拆分的问题
    Oracle trunc() 函数处理数字、日期的整理
    左连接条件与where条件的区别
  • 原文地址:https://www.cnblogs.com/lz87/p/7097045.html
Copyright © 2011-2022 走看看