diff options
author | Jack Sangdahl <[email protected]> | 2025-02-08 18:00:46 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-02-08 18:00:46 -0700 |
commit | 38628b7a294223f7e1caa129f55753183a536fa6 (patch) | |
tree | 5939376ce83a3558c0b76bc8c995f7223809edc5 /num_bad_pairs.py | |
parent | f72076c568e27c480a1154b527433077e3884aad (diff) | |
download | leetcode-38628b7a294223f7e1caa129f55753183a536fa6.tar.gz leetcode-38628b7a294223f7e1caa129f55753183a536fa6.zip |
2364. count number of bad pairs
Diffstat (limited to 'num_bad_pairs.py')
-rw-r--r-- | num_bad_pairs.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/num_bad_pairs.py b/num_bad_pairs.py new file mode 100644 index 0000000..6f3ae05 --- /dev/null +++ b/num_bad_pairs.py @@ -0,0 +1,27 @@ +# 2364. Count Number of Bad Pairs +from collections import defaultdict + +""" +you are given a 0-indexed integer array 'nums'. a pair of indices '(i, j)' is +a bad pair if 'i < j' and 'j - i != nums[j] - nums[i]'. return the total +number of bad pairs in 'nums'. +""" + + +class Solution(object): + def countBadPairs(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + mp, ans = defaultdict(int), 0 + for i in range(len(nums)): + ans += i - mp[nums[i] - i] + mp[nums[i] - i] += 1 + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.countBadPairs(nums=[4, 1, 3, 3])) + print(obj.countBadPairs(nums=[1, 2, 3, 4, 5])) |