Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def intervalIntersection(self, A: List[List[int]], B: List[List[int]]) -> List[List[int]]:
- ans = []
- i, j = 0, 0 # look at two intervals at a time, A[i] and B[j]
- while i < len(A) and j < len(B):
- # find bounds of intersection (if two intervals intersect)
- lower = max(A[i][0], B[j][0])
- higher = min(A[i][1], B[j][1])
- if lower <= higher:
- # is an intersection; add to answer
- ans += [[lower, higher]]
- # now look at a different pair of intervals
- if A[i][1] < B[j][1]: # A[i] ended before B[j]
- i += 1
- elif A[i][1] > B[j][1]: # B[j] ended before A[i]
- j += 1
- else: # note if both intervals end at the same time, both i and j increments
- i += 1
- j += 1
- return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement