[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

转载于:https://www.cnblogs.com/lz87/p/7097045.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值