Max Pair双指针
Description
Example
Explanation:
the sum of [3,5] or [4,4] is 8, which is no larger than 8.Explanation:
the sum of [3,7],[5,5],[6,4] is 10, which is no larger than 10.class Solution:
"""
@param a: the first list
@param b: the second list
@param x: the max sum
@return: the pairs whose sum are not exceed x
"""
def getAns(self, a, b, x):
# Write your code here.
a.sort()
b.sort()
s = 0
e = len(b) - 1
res = []
mindiff = float('inf')
while s < len(a) and e >= 0:
subsum = a[s] + b[e]
d = x - subsum
if d == 0:
if d == mindiff:
res.append([a[s] , b[e]])
else:
res = [[a[s] , b[e]]]
mindiff = d
s+=1
e-=1
elif d > 0:
if d < mindiff:
res = [[a[s] , b[e]]]
mindiff = d
elif d == mindiff:
res.append([a[s] , b[e]])
s += 1
else:
e -= 1
return resLast updated