-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path986_interval-list-intersections.py
32 lines (30 loc) · 1.51 KB
/
986_interval-list-intersections.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from typing import List
class Solution:
def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
rs = []
while len(firstList) > 0 and len(secondList) > 0:
if firstList[0][0] < secondList[0][0]:
if firstList[0][1] < secondList[0][0]:
firstList.pop(0)
continue
elif firstList[0][1] >= secondList[0][0]:
rs.append([secondList[0][0], firstList[0][1] if firstList[0][1] <= secondList[0][1] else secondList[0][1]])
secondList.pop(0) if firstList[0][1] > secondList[0][1] else firstList.pop(0)
continue
elif secondList[0][0] < firstList[0][0]:
if secondList[0][1] < firstList[0][0]:
secondList.pop(0)
continue
elif secondList[0][1] >= firstList[0][0]:
rs.append([firstList[0][0], secondList[0][1] if firstList[0][1] >= secondList[0][1] else firstList[0][1]])
secondList.pop(0) if firstList[0][1] > secondList[0][1] else firstList.pop(0)
continue
else:
if firstList[0][1] > secondList[0][1]:
rs.append([secondList[0][0], secondList[0][1]])
secondList.pop(0)
continue
else:
rs.append([firstList[0][0], firstList[0][1]])
firstList.pop(0)
return rs