Given a 0-indexed integer array nums of size n and two integers lower and upper, return the number of fair pairs.
A pair (i, j) is fair if:
0 <= i < j < n, and
lower <= nums[i] + nums[j] <= upper
[0,1,7,4,4,5]
총 6개의 쌍이 존재한다.
class Solution:
def countFairPairs(self, nums: List[int], lower: int, upper: int) -> int:
nums.sort()
ans = 0
for index, left in enumerate(nums):
l = bisect_left(nums, lower - left)
r = bisect_right(nums, upper - left)
l = max(l, index + 1)
diff = r - l
ans += max(diff, 0)
return ans