-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path986. Interval List Intersections.py
35 lines (27 loc) · 1.25 KB
/
986. Interval List Intersections.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Explanation: https://leetcode.com/problems/interval-list-intersections/discuss/647482/Python-Two-Pointer-Approach-%2B-Thinking-Process-Diagrams
# Time O(N+M)
class Solution(object):
def intervalIntersection(self, firstList, secondList):
"""
:type firstList: List[List[int]]
:type secondList: List[List[int]]
:rtype: List[List[int]]
"""
lengthA, lengthB = len(firstList), len(secondList)
if lengthA == 0 or lengthB == 0:
return []
i,j,result = 0,0,[]
while i< lengthA and j<lengthB:
aStart, aEnd = firstList[i][0],firstList[i][1]
bStart, bEnd = secondList[j][0],secondList[j][1]
if self.isCrissCrossExist(aStart, aEnd, bStart, bEnd):
result.append(self.getCrossSection(aStart, aEnd, bStart, bEnd))
if aEnd >= bEnd:
j = j + 1
else:
i = i + 1
return result
def isCrissCrossExist(self, aStart, aEnd, bStart, bEnd):
return aStart <= bEnd and bStart <= aEnd
def getCrossSection(self, aStart, aEnd, bStart, bEnd):
return [ max(aStart,bStart), min(aEnd,bEnd) ]