-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0986_Interval_List_Intersections.py
More file actions
41 lines (34 loc) · 1.51 KB
/
0986_Interval_List_Intersections.py
File metadata and controls
41 lines (34 loc) · 1.51 KB
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
33
34
35
36
37
38
39
40
41
class Solution:
def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
index_2 = 0
sol = []
for start, end in firstList:
while index_2 < len(secondList) and secondList[index_2][1] < start:
index_2 += 1
if not index_2 < len(secondList):
break
# B Start < A Start
if secondList[index_2][0] < start:
# B Start < A Start < B End < A End
if secondList[index_2][1] >= start and secondList[index_2][1] <= end:
sol.append([start, secondList[index_2][1]])
index_2 += 1
# B Start < A Start < A End < B End
elif secondList[index_2][1] > end:
sol.append([start, end])
continue
# A Start < B Start
# second index stretch
while index_2 < len(secondList) and start > secondList[index_2][1]:
index_2 += 1
# A Start < B Start
while index_2 < len(secondList) and secondList[index_2][0] <= end:
# A Start < B Start < B End < A End
if secondList[index_2][1] <= end:
sol.append(secondList[index_2])
# A Start < B Start < A End < B End
else:
sol.append([secondList[index_2][0], end])
break
index_2 += 1
return sol