leetcode 986. 区间列表的交集
给定两个由一些 闭区间 组成的列表,每个区间列表都是成对不相交的,并且已经排序。
返回这两个区间列表的交集。
(形式上,闭区间 [a, b](其中 a <= b)表示实数 x 的集合,而 a <= x <= b。两个闭区间的交集是一组实数,要么为空集,要么为闭区间。例如,[1, 3] 和 [2, 4] 的交集为 [2, 3]。)
示例:
输入:A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
输出:[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
提示:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
思路
由于区间已经排好序,所以先拿出A中的第一个区间和B中第一个区间,判断有没有交集,选取左端点的最大值作为start和右端点的最小值作为end,如果start<=end,那么就存在交集,将其放到结果文件中,然后判断A中的第一个区间和B中第一个区间那一个比较长,也就是右端点哪一个较大,如果A大,那么选取B的下一个,如果B大,那么选取A的下一个,依次比较即可.
好了,看代码
class Solution:
def intervalIntersection(self, A: List[List[int]], B: List[List[int]]) -> List[List[int]]:
m = len(A)
n = len(B)
p1 = 0
p2 = 0
res = []
while p1 < m and p2 < n:
a1 = A[p1][0]
a2 = A[p1][1]
b1 = B[p2][0]
b2 = B[p2][1]
start = max(a1, b1)
end = min(a2, b2)
if start <= end:
res.append([start, end])
if a2 < b2:
p1 += 1
else:
p2 += 1
return res
leetcode 56. 合并区间
给出一个区间的集合,请合并所有重叠的区间。
示例 1:
输入: [[1,3],[2,6],[8,10],[15,18]]
输出: [[1,6],[8,10],[15,18]]
解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
示例 2:
输入: [[1,4],[4,5]]
输出: [[1,5]]
解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。
思路
首先将区间按照左端点大小进行排序,然后现将第一个区间放到结果res中,然后遍历后面的区间,如果当前区间的左端点比结果res的最后一个区间的右端点小 inter[0] <= res[-1][1]
,那么会产生重合区间,重叠区间的左端点是res中最后一个区间的左端点left = res[-1][0]
,重叠区间的右端点是当前区间和res最后一个区间的右端点的最大值right = max(res[-1][1], inter[1])
,之后将res[-1]从结果中删除,将重叠区间放进去,如果当前区间的左端点比结果res的最后一个区间的右端点大,那么不可能产生重叠,直接将区间放进res中
好了,看代码
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
res = []
if not intervals:
return res
intervals.sort()
res.append(intervals[0])
for inter in intervals[1:]:
if inter[0] <= res[-1][1]:
left = res[-1][0]
right = max(res[-1][1], inter[1])
res.pop()
else:
left = inter[0]
right = inter[1]
res.append([left, right])
return res
leetcode 57. 插入区间
给出一个无重叠的 ,按照区间起始端点排序的区间列表。
在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。
示例 1:
输入:intervals = [[1,3],[6,9]], newInterval = [2,5]
输出:[[1,5],[6,9]]
示例 2:
输入:intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
输出:[[1,2],[3,10],[12,16]]
解释:这是因为新的区间 [4,8] 与 [3,5],[6,7],[8,10] 重叠。
思路
由于,区间是排好序的,所以可以先找到开始重叠的区间位置temp,对于重叠之前的区间保持不变,当发生重叠时,进行区间的合并,,直到后面的区间不再重叠,此时可以删除中间重叠部分的区间,最后将合并好的区间插入到temp位置
区间分为三段
第一段为前半部分不重叠的,判断条件为:intervals[i][1] < newInterval[0]
第二段为重叠部分,判断条件为:intervals[i][0] <= newInterval[1]
并且intervals[i][1] >= newInterval[0]
第三部分为后半部分不重叠的,判断条件为:intervals[i][0] > newInterval[1]
class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
if not newInterval:
return intervals
if not intervals:
return [newInterval]
i = 0
length = len(intervals)
while i < length and intervals[i][1] < newInterval[0]: # 前半部分不重叠的
i += 1
temp = i # 记录开始重叠的位置
while i < length and intervals[i][0] <= newInterval[1]: # 出现重叠,注意此时可以省略intervals[i][1] >= newInterval[0]这个条件,因为上一个while循环已经结束了
newInterval[0] = min(intervals[i][0], newInterval[0])
newInterval[1] = max(intervals[i][1], newInterval[1]) # 更新重叠区间的数值
i += 1
# while 循环终止,说明不再出现重叠,也即intervals[i][0] > newInterval[1]
del intervals[temp:i] # 删除中间重叠的区间
intervals.insert(temp, newInterval) # 将合并好的区间插入到temp位置
return intervals