Algorithm/python tip
1679. Max Number of K-Sum Pairs
개구리는 개꿀개꿀
2021. 3. 1. 18:13
leetcode.com/problems/max-number-of-k-sum-pairs/
Max Number of K-Sum Pairs - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
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
collections 모듈 - defaultdict
collections.defaultdict 1. defaultdict란 collections.defaultdict 는 딕셔너리( dictionary )와 거의 비슷하지만 key값이 없을 경우 미리 지정해 놓은 초기(default)값을 반환하는 dictionary 이다. defaultdic..
excelsior-cjh.tistory.com