码迷,mamicode.com
首页 > 其他好文 > 详细

Insert Interval & Merge Intervals

时间:2016-07-14 07:11:02      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

Insert Intervals

Given a non-overlapping interval list which is sorted by start point.

Insert a new interval into it, make sure the list is still in order and non-overlapping (merge intervals if necessary).

Example

Insert [2, 5] into [[1,2], [5,9]], we get [[1,9]].

Insert [3, 4] into [[1,2], [5,9]], we get [[1,2], [3,4], [5,9]].

分析:

Create a new array list, insert the smaller interval in the new array and use a counter to keep track of the total number of smaller intervals. If we find an interval overlapping with the new one, we need to change the start and end.

 1 /**
 2  * Definition of Interval:
 3  * public classs 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      * Insert newInterval into intervals.
14      * @param intervals: Sorted interval list.
15      * @param newInterval: A new interval.
16      * @return: A new sorted interval list.
17      */
18     
19     public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
20         if (newInterval == null || intervals == null) {
21             return intervals;
22         }
23 
24         ArrayList<Interval> results = new ArrayList<Interval>();
25         int insertPos = 0;
26 
27         for (Interval interval : intervals) {
28             if (interval.end < newInterval.start) {
29                 results.add(interval);
30                 insertPos++;
31             } else if (interval.start > newInterval.end) {
32                 results.add(interval);
33             } else {
34                 newInterval.start = Math.min(interval.start, newInterval.start);
35                 newInterval.end = Math.max(interval.end, newInterval.end);
36             }
37         }
38 
39         results.add(insertPos, newInterval);
40         return results;
41 
42     }
43 }

 

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]            ]
]

Analyze:

Sort first, then merge intervals if they overlap.

 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     public List<Interval> merge(List<Interval> list) {
17         
18         if (list == null || list.size() <= 1) {
19             return list;
20         }
21         
22         Collections.sort(list, new Comparator<Interval>(){
23             public int compare(Interval a, Interval b) {
24                 return a.start - b.start;
25         }});
26         
27         for (int i = 1; i < list.size(); i++) {  
28             //no intersection  
29             if (overlap(list.get(i), list.get(i - 1))) { 
30                 list.get(i - 1).start = Math.min(list.get(i).start, list.get(i - 1).start);  
31                 list.get(i - 1).end = Math.max(list.get(i).end, list.get(i - 1).end); 
32                 list.remove(i);
33                 i--;
34             }
35         }  
36         return list;
37     }
38     
39     boolean overlap(Interval i1, Interval i2) {  
40         return Math.max(i1.start, i2.start) <= Math.min(i1.end, i2.end);  
41     } 
42 }

 

 

Insert Interval & Merge Intervals

标签:

原文地址:http://www.cnblogs.com/beiyeqingteng/p/5668863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!