這一題應該算是時間區間的最後一個變形,可以想像成,有兩個人的班表,我們要找出他們哪些時間有一起上班,可能是這個時間這兩個人才可以開會,如果說這個題目問超過兩個人,其實也不會太難,就很像是 N sum 或是 Merger K Linked List 的題目一樣,兩兩的班表慢慢合併,再找出所有重複的區間就可以。
classSolution:defintervalIntersection(self,firstList: List[List[int]],secondList: List[List[int]])-> List[List[int]]:ifnot firstList ornot secondList:return[] i =0 j =0 res =[]while i <len(firstList)and j <len(secondList): lo =max(firstList[i][0], secondList[j][0]) hi =min(firstList[i][1], secondList[j][1])if lo <= hi: res.append([lo, hi])if firstList[i][1]< secondList[j][1]: i +=1else: j +=1return res