728x90
https://leetcode.com/problems/number-of-good-pairs/
class Solution:
def numIdenticalPairs(self, nums: List[int]) -> int:
nums_dic = {}
for index, num in enumerate(nums):
now = nums_dic.get(num, [])
now.append(index)
nums_dic[num] = now
count = 0
for key, value in nums_dic.items():
count += math.comb(len(value), 2)
return count
728x90
'IT > 코딩테스트' 카테고리의 다른 글
[Leetcode] 1282. Group the People Given the Group Size They Belong To (0) | 2024.02.08 |
---|---|
[Leetcode] 771. Jewels and Stones (0) | 2024.02.07 |
[Leetcode] 2671. Frequency Tracker (0) | 2024.02.07 |
[Leetcode] 2236. Root Equals Sum of Children (0) | 2024.02.05 |
[Leetcode] 1002. Find Common Characters (0) | 2024.02.04 |