Facebook
From bill, 1 Month ago, written in Python.
Embed
Download Paste or View Raw
Hits: 140
  1. class Solution:
  2.     def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
  3.         flp, slp = 0,0
  4.         overlaps = []
  5.         while flp < len(firstList) and slp < len(secondList):
  6.             m1 = firstList[flp]
  7.             m2 = secondList[slp]
  8.  
  9.             overlap = [max(m1[0], m2[0]), min(m1[1], m2[1])]
  10.            
  11.             if overlap[0] <= overlap[1]:
  12.                 # found valid overlap
  13.                 overlaps.append(overlap)
  14.            
  15.             if m1[1] <= m2[1]:
  16.                 flp+=1
  17.             else:
  18.                 slp+=1
  19.        
  20.         return overlaps