1679. Max Number of K-Sum Pairs

Solution:

  • using a defaultdict make things much easier.
  • i can keep a count variable, incrementing every match.
  • gotta make sure to remove the diff amount if exists in d
class Solution:
    def maxOperations(self, nums: List[int], k: int) -> int:
        d = defaultdict(int)
        count = 0
        for num in nums:
            diff = k - num
            if diff in d and d[diff] > 0:
                count += 1
                d[diff] -= 1
            else:
                d[num] += 1

        return count