986. Interval List Intersections
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 73% Topics: Array, Two Pointers, Line Sweep
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(m + n)
# Space: O(1)
# Definition for an interval.
class Interval(object):
def __init__(self, s=0, e=0):
self.start = s
self.end = e
class Solution(object):
def intervalIntersection(self, A, B):
"""
:type A: List[Interval]
:type B: List[Interval]
:rtype: List[Interval]
"""
result = []
i, j = 0, 0
while i < len(A) and j < len(B):
left = max(A[i].start, B[j].start)
right = min(A[i].end, B[j].end)
if left <= right:
result.append(Interval(left, right))
if A[i].end < B[j].end:
i += 1
else:
j += 1
return result
Solution from kamyu104/LeetCode-Solutions · MIT