Skip to content

Commit 10b7fce

Browse files
committed
Added Python solution for "Interval List Intersections"
1 parent f837a2b commit 10b7fce

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Python/Interval List Intersections.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""
6+
Time: O(n + m)
7+
Memory: O(n + m)
8+
"""
9+
10+
def intervalIntersection(self, first: List[List[int]], second: List[List[int]]) -> List[List[int]]:
11+
i = j = 0
12+
intersection = []
13+
14+
while i < len(first) and j < len(second):
15+
a_start, a_end = first[i]
16+
b_start, b_end = second[j]
17+
18+
start, end = max(a_start, b_start), min(a_end, b_end)
19+
if start <= end:
20+
intersection.append([start, end])
21+
22+
if a_end < b_end:
23+
i += 1
24+
else:
25+
j += 1
26+
27+
return intersection
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)