1679.K和数对的最大数目
先排序,然后双指针法
class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
nums.sort()
n = len(nums)
res = 0
l, r = 0, n-1
while l < r:
if nums[l] + nums[r] < k:
l += 1
elif nums[l] + nums[r] > k:
r -= 1
else:
res += 1
l += 1
r -= 1
return res
最后更新于