본문 바로가기

IT/코딩테스트

[Leetcode] 1512. Number of Good Pairs

728x90

https://leetcode.com/problems/number-of-good-pairs/

 

LeetCode - The World's Leading Online Programming Learning Platform

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

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