986. Interval List Intersections

https://leetcode.com/problems/interval-list-intersections/

Two Pointer Approach

Input:

[[0,2],[5,10],[13,23],[24,25]]

[[1,5],[8,12],[15,24],[25,26]]

Output:

[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]

class Solution:
    def intervalIntersection(self, A: List[List[int]], B: List[List[int]]) -> List[List[int]]:
        i = 0
        j = 0
        res = []
        while i < len(A) and j < len(B):
            startMax = max(A[i][0],B[j][0])
            endMin = min(A[i][1],B[j][1])
            
            if startMax <= endMin:
                res.append([startMax,endMin])
            
            if A[i][1] == endMin:
                i+=1
            else:
                j+=1
        
        return res

Last updated

Was this helpful?