Soohan-Kim / Leetcode

0 stars 0 forks source link

[HW 3] Interval List Intersections #986. #36

Open Soohan-Kim opened 3 weeks ago

Soohan-Kim commented 3 weeks ago

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

Soohan-Kim commented 3 weeks ago
class Solution:
    def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
        res=[]
        i=0
        j=0
        while i<len(firstList) and j<len(secondList):
            start=max(firstList[i][0],secondList[j][0])
            end=min(firstList[i][1],secondList[j][1])
            if start<=end:
                res.append([start,end])
            if firstList[i][1]<secondList[j][1]:
                i+=1
            else:
                j+=1
        return res