-
1679. Max Number of K-Sum PairsAlgorithm/python tip 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
'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