2006. Count Number of Pairs With Absolute Difference K

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 2006

class Solution:
    def countKDifference(self, nums: List[int], k: int) -> int:
        res = 0
        cnt = Counter()
        for num in nums:
            res += cnt[num - k] + cnt[num + k]
            cnt[num] += 1
        return res