Advertisement
DeepRest

Max Number of K-Sum Pairs

May 4th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. class Solution:
  2.     def maxOperations(self, nums: List[int], k: int) -> int:
  3.         freqMap = {}
  4.         for x in nums:
  5.             freqMap[x] = freqMap.get(x, 0) + 1
  6.            
  7.         pairs = 0
  8.         for first in freqMap:
  9.             second = k - first
  10.            
  11.             if first == second:
  12.                 pairs += freqMap.get(first, 0)//2
  13.             else:
  14.                 pairs += min(freqMap.get(first, 0), freqMap.get(second, 0))
  15.            
  16.             freqMap[first] = 0
  17.            
  18.         return pairs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement