-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathinterval-list-intersections.py
43 lines (35 loc) · 1.25 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
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
ptr_a, ptr_b = 0, 0
result = []
def bump_left(ptr_a, ptr_b):
if A[ptr_a] <= B[ptr_b]:
ptr_a += 1
else:
ptr_b += 1
return ptr_a, ptr_b
def bump_right(ptr_a, ptr_b):
if A[ptr_a] <= B[ptr_b]:
ptr_b += 1
else:
ptr_a += 1
return ptr_a, ptr_b
while ptr_a < len(A) and ptr_b < len(B):
if A[ptr_a] <= B[ptr_b]:
left, right = A[ptr_a], B[ptr_b]
else:
left, right = B[ptr_b], A[ptr_a]
left_start, left_end = left
right_start, right_end = right
if left_end >= right_start:
if left_end >= right_end:
result.append([right_start, right_end])
ptr_a, ptr_b = bump_right(ptr_a, ptr_b)
elif left_end < right_end:
result.append([right_start, left_end])
ptr_a, ptr_b = bump_left(ptr_a, ptr_b)
else:
ptr_a, ptr_b = bump_left(ptr_a, ptr_b)
return result