-
1679. Max Number of K-Sum PairsAlgorithm/python tip 2021. 3. 1. 18:13
leetcode.com/problems/max-number-of-k-sum-pairs/
from collections import Counter class Solution(object): def maxOperations(self, nums, k): dic = Counter(nums) ans = 0 for n in nums: if k - n in dic: if n == k - n: if dic[n] >= 2: dic[n] -= 2 ans += 1 elif dic[n] > 0 and dic[k - n] > 0: dic[n] -= 1 dic[k - n] -= 1 ans += 1 return ans
collections.defaultdict
'Algorithm > python tip' 카테고리의 다른 글
1588. Sum of All Odd Length Subarrays (0) 2021.03.01 1732. Find the Highest Altitude (0) 2021.03.01 137. Single Number II (0) 2021.03.01 set을 만들지 않고 list 차집합 구하기(for in if not in) (0) 2021.03.01 1726. Tuple with Same Product (0) 2021.03.01