leetpycode/medium/count_number_of_bad_pairs.py

13 lines
400 B
Python

# https://leetcode.com/problems/count-number-of-bad-pairs
from typing import List
class Solution:
def countBadPairs(self, nums: List[int]) -> int:
result = 0
counts = {}
for i in range(len(nums)):
zero_value = nums[i] - i
counts[zero_value] = counts.get(zero_value, 0) + 1
result += (i + 1) - counts[zero_value]
return result