2563. Count the Number of Fair Pairs ## 思路 任兩數相加介於lower, upper之間 先排序 用Two pointer分別計算小於等於upper跟lower-1 Pair個數並相減 ## Code ``` class Solution: def countFairPairs(self, nums: List[int], lower: int, upper: int) -> int: nums.sort() n = len(nums) def get_count(val): res = 0 left, right = 0, n-1 while left < right: if nums[left] + nums[right] <= val: res += right - left left += 1 else: right -= 1 return res return get_count(upper) - get_count(lower-1) ``` -- ※ 發信站: 批踢踢實業坊(ptt-site.org.tw), 來自: 94.156.205.47 (臺灣) ※ 文章網址: https://ptt-site.org.tw/Marginalman/M.1731497147.A.2B7