Input: firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Input: firstList = [[1,3],[5,9]], secondList = []
Output: []
조건
0 <= firstList.length, secondList.length <= 1000
firstList.length + secondList.length >= 1
0 <= starti < endi <= 109
endi < starti+1
0 <= startj < endj <= 109
endj < startj+1
class Solution:
def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
n, m = len(firstList), len(secondList)
l, r = 0, 0
ans = []
while l < n and r < m:
out_list1 = max(firstList[l][0], secondList[r][0])
out_list2 = min(firstList[l][1], secondList[r][1])
if out_list2 - out_list1 >= 0:
ans.append([out_list1, out_list2])
if firstList[l][1] > secondList[r][1]:
r += 1
else:
l += 1
return ans