-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathInterval List Intersections.py
90 lines (75 loc) · 3.06 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Runtime: 325 ms (Top 13.56%) | Memory: 15.2 MB (Top 15.52%)
from collections import deque
class Solution:
def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
answer = []
if len(firstList) == 0 or len(secondList) == 0:
return answer
first_queue= deque(firstList)
second_queue = deque(secondList )
first = first_queue.popleft()
second = second_queue.popleft()
while first_queue or second_queue:
if first[1] < second[0]:
if len(first_queue):
first = first_queue.popleft()
continue
break
if second[1] < first[0]:
if len(second_queue) > 0:
second = second_queue.popleft()
continue
break
if first[0] <= second[0] and second[0] <= first[1]:
if first[1] <= second[1]:
answer.append([second[0], first[1]])
if len(first_queue) > 0:
first = first_queue.popleft()
continue
break
else:
answer.append(second)
if len(second_queue) > 0:
second = second_queue.popleft()
continue
break
if second[0] <= first[0] and first[0] <= second[1]:
if second[1] <= first[1]:
answer.append([first[0], second[1]])
if len(second_queue) > 0:
second = second_queue.popleft()
continue
break
else:
answer.append(first)
if len(first_queue) > 0:
first = first_queue.popleft()
continue
break
if first[0] <= second[0] and second[0] <= first[1]:
if first[1] <= second[1]:
if len(answer) > 0:
if answer[-1] != [second[0], first[1]]:
answer.append([second[0], first[1]])
elif not answer:
answer.append([second[0], first[1]])
else:
if len(answer) > 0:
if answer[-1] != second:
answer.append(second)
elif not answer:
answer.append(second)
elif second[0] <= first[0] and first[0] <= second[1]:
if second[1] <= first[1]:
if len(answer) > 0:
if answer[-1] != [first[0], second[1]]:
answer.append([first[0], second[1]])
elif not answer:
answer.append([first[0], second[1]])
else:
if len(answer) > 0:
if answer[-1] != first:
answer.append(first)
elif not answer:
answer.append(first)
return answer