Skip to content

Instantly share code, notes, and snippets.

@vrat28
Created May 14, 2021 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/16bcd1aec901fbb03c8347b90cb85737 to your computer and use it in GitHub Desktop.
Save vrat28/16bcd1aec901fbb03c8347b90cb85737 to your computer and use it in GitHub Desktop.
Merge Intervals Python
class Solution:
def intervalIntersection(self, A: List[List[int]], B: List[List[int]]) -> List[List[int]]:
ans = []
i = j = 0
while i < len(A) and j < len(B):
# Let's check if A[i] intersects B[j].
# lo - the startpoint of the intersection
# hi - the endpoint of the intersection
lo = max(A[i][0], B[j][0])
hi = min(A[i][1], B[j][1])
if lo <= hi:
ans.append([lo, hi])
# Remove the interval with the smallest endpoint
if A[i][1] < B[j][1]:
i += 1
else:
j += 1
return ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment