Created
May 14, 2021 03:25
-
-
Save vrat28/16bcd1aec901fbb03c8347b90cb85737 to your computer and use it in GitHub Desktop.
Merge Intervals Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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