class Solution:
def intervalIntersection(self, A: List[List[int]], B: List[List[int]]) -> List[List[int]]:
"""
tc O(A+B) sc O(1)
main idea: use two pointer to track overlapping area between two intervels. once one intervel exhaust, move to next one
assuming both A,B list have overlaps
1. check corner case: one of them empty
2. use criss-cross lock to check overlaps, update res
3. no matter if any overlap found, move exhausted intervel pointer to next
"""
n1,n2 = len(A), len(B)
# early termination
if not n1 or not n2:
return []
p1,p2 = 0,0
res = []
while p1 < n1 and p2 < n2:
As,Ae = A[p1]
Bs,Be = B[p2]
# there is an overlap --- Criss-cross lock
if Bs <= Ae and As <=Be :
res.append([max(As,Bs),min(Ae,Be)])
if Ae <= Be:
p1 += 1
else: # Be < Ae
p2 += 1
return res