|
1 | 1 | # Merge Intervals 区间合并问题
|
2 | 2 |
|
3 |
| -## 模板题1 - Merge Intevals |
| 3 | +## 模板题1 - Merge Intervals |
4 | 4 |
|
5 | 5 | #### 题目
|
6 | 6 |
|
@@ -64,3 +64,77 @@ class Solution(object):
|
64 | 64 | return merged_intervals
|
65 | 65 | ```
|
66 | 66 |
|
| 67 | +## 模板题2 - Insert Interval |
| 68 | + |
| 69 | +#### 题目 |
| 70 | + |
| 71 | +Given a list of non-overlapping intervals **sorted** by their start time, **insert a given interval at the correct position** and merge all necessary intervals to produce a list that has only mutually exclusive intervals. |
| 72 | + |
| 73 | +**Example 1:** |
| 74 | + |
| 75 | +``` |
| 76 | +Input: Intervals=[[1,3], [5,7], [8,12]], New Interval=[4,6] |
| 77 | +Output: [[1,3], [4,7], [8,12]] |
| 78 | +Explanation: After insertion, since [4,6] overlaps with [5,7], we merged them into one [4,7]. |
| 79 | +``` |
| 80 | + |
| 81 | +**Example 2:** |
| 82 | + |
| 83 | +``` |
| 84 | +Input: Intervals=[[1,3], [5,7], [8,12]], New Interval=[4,10] |
| 85 | +Output: [[1,3], [4,12]] |
| 86 | +Explanation: After insertion, since [4,10] overlaps with [5,7] & [8,12], we merged them into [4,12]. |
| 87 | +``` |
| 88 | + |
| 89 | +**Example 3:** |
| 90 | + |
| 91 | +``` |
| 92 | +Input: Intervals=[[2,3],[5,7]], New Interval=[1,4] |
| 93 | +Output: [[1,4], [5,7]] |
| 94 | +Explanation: After insertion, since [1,4] overlaps with [2,3], we merged them into one [1,4]. |
| 95 | + |
| 96 | +``` |
| 97 | + |
| 98 | +总结:对于一个**排好序**的区间序列,插入一个新区间,返回插入且合并后的区间结果。 |
| 99 | + |
| 100 | +#### 思路 |
| 101 | + |
| 102 | +- 如果用上一道题 Merge Intervals 的方法,先排序,再合并,时间复杂度为 O(N*logN)。但考虑到本题所给区间序列已排好序,我们尝试用 O(N) 的办法一趟完成合并。 |
| 103 | + |
| 104 | +#### 图解&算法 |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +- 对第一种情况:也就是 a.end < b.start 的区间,将这些区间直接加入到 result 并跳过即可,直到发现有 overlap 的区间为止。 |
| 109 | +- 对剩余四种情况,共同特点 a.start < b.end,即必有交集,需要合并: |
| 110 | + - 合并后的起点 start,谁在前,谁是 start,即 ***min***(a.start, b.start) |
| 111 | + - 合并后的终点 end,谁在后,谁是 end,即 ***max***(a.end, b.end) |
| 112 | + |
| 113 | +#### 代码 |
| 114 | + |
| 115 | +```python |
| 116 | +class Solution(object): |
| 117 | + def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]: |
| 118 | + |
| 119 | + merged, i, start, end = [], 0, 0, 1 |
| 120 | + while i < len(intervals) and intervals[i][end] < newInterval[start]: |
| 121 | + # skip these intervals and append them into merged[] |
| 122 | + merged.append(intervals[i]) |
| 123 | + i += 1 |
| 124 | + |
| 125 | + # already find interval do not follow the condition |
| 126 | + while i < len(intervals) and intervals[i][start] <= newInterval[end]: |
| 127 | + newInterval[start] = min(newInterval[start], intervals[i][start]) # 谁小谁起点 |
| 128 | + newInterval[end] = max(newInterval[end], intervals[i][end]) # 谁大谁终点 |
| 129 | + i += 1 |
| 130 | + |
| 131 | + merged.append(newInterval) # 合并完,加到merged |
| 132 | + |
| 133 | + # for the rest of intervals |
| 134 | + while i < len(intervals): |
| 135 | + merged.append(intervals[i]) |
| 136 | + i += 1 |
| 137 | + |
| 138 | + return merged |
| 139 | +``` |
| 140 | + |
0 commit comments