Insert Interval & Merge Intervals

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 }
 
时间: 2024-11-05 12:35:28

Insert Interval & Merge Intervals的相关文章

60. Insert Interval &amp;&amp; Merge Intervals

Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], i

LeetCode 57. Insert Interval / Add Interval (get total covered length)

57. Insert Interval 由于intervals已经有序,不需要排序.本题是要返回一个interval数组,所以并不需要对原数组进行改动. 方法一: 由于intervals有序,我们可以二分找到应该插入的位置,然后merge intervals即可. 时间复杂度 O(n) 方法二: 我们只需要找出overlap的部分,merge即可.别的没有overlap的部分保持不变. class Solution { public: vector<vector<int>> ins

56. Merge Intervals 57. Insert Interval *HARD*

1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. /** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0)

leetcode || 56、 Merge Intervals

problem: Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. Hide Tags Array Sort 题意:给定数组区间,合并有覆盖或者相邻的区间 thinking: (1)一開始我想到用hash table的方法,开一个总区间跨度的数组.对于有区间覆盖的数

LeetCode57 Insert Interval

题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1:Given intervals [1,3],[6,9], insert and mer

[LeetCode][Java] Insert Interval

题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and me

LeetCode --- 57. Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge

每日算法之四十:Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge 

57. Insert Interval

Total Accepted: 83528 Total Submissions: 314574 Difficulty: Hard Contributors: Admin Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted acco