max-number-of-k-sum-pairs py3
This commit is contained in:
21
1798_max-number-of-k-sum-pairs/python3/solution.py
Normal file
21
1798_max-number-of-k-sum-pairs/python3/solution.py
Normal file
@@ -0,0 +1,21 @@
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user