Skip to main content

986. Interval List Intersections

Python

Time Exceed Try

class Solution:
def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
if not firstList or not secondList:
return []

results = []
result = []

uppbound = max(firstList[-1][1], secondList[-1][1])+1
lowerbound = min(firstList[0][0], secondList[0][0])
first_current = None
second_current = None

for num in range(lowerbound, uppbound):
if not first_current or num > first_current[1]:
if firstList:
first_current = firstList.pop(0)
if result:
results.append(result)
result = []

if not second_current or num > second_current[1]:
if secondList:
second_current = secondList.pop(0)
if result:
results.append(result)
result = []

first_enabled = (num >= first_current[0] and num <= first_current[1])
second_enabled = (num >= second_current[0] and num <= second_current[1])

if first_enabled and second_enabled:
if not result:
result = [num, num]
else:
result[1] = num

if (not first_enabled or not second_enabled) and result:
results.append(result)
result = []


if result:
results.append(result)

return results