leetcode/1798_max-number-of-k-sum-pairs/python3/solution.py

21 lines
510 B
Python
Raw Normal View History

2022-05-05 02:22:58 +00:00
class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
nums.sort()
left, right = 0, len(nums) - 1
count = 0
while left < right:
total = nums[left] + nums[right]
if total == k:
count += 1
left += 1
right -= 1
elif total < k:
left += 1
else:
right -= 1
return count