Skip to content

Latest commit

 

History

History
25 lines (23 loc) · 920 Bytes

986. Interval List Intersections.md

File metadata and controls

25 lines (23 loc) · 920 Bytes

986. Interval List Intersections

Algorithm: Scan line and two pointers

Simiar as 1229. Meeting Scheduler

class Solution(object):
    def intervalIntersection(self, firstList, secondList):
        """
        :type firstList: List[List[int]]
        :type secondList: List[List[int]]
        :rtype: List[List[int]]
        """
        i = j = 0
        ans = []
        while(i < len(firstList) and j < len(secondList)):
            startOverlap = max(firstList[i][0], secondList[j][0])
            endOverlap = min(firstList[i][1], secondList[j][1])
            print(f"[{startOverlap}, {endOverlap}]")
            if endOverlap >= startOverlap:
                ans.append([startOverlap, endOverlap])
            if firstList[i][1] > secondList[j][1]: j += 1 
            else: i += 1
            
        return ans