Problem

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.

https://leetcode.com/problems/count-number-of-bad-pairs/

Example 1:

Input: nums = [4,1,3,3]
Output: 5
Explanation: The pair (0, 1) is a bad pair since 1 - 0 != 1 - 4.
The pair (0, 2) is a bad pair since 2 - 0 != 3 - 4, 2 != -1.
The pair (0, 3) is a bad pair since 3 - 0 != 3 - 4, 3 != -1.
The pair (1, 2) is a bad pair since 2 - 1 != 3 - 1, 1 != 2.
The pair (2, 3) is a bad pair since 3 - 2 != 3 - 3, 1 != 0.
There are a total of 5 bad pairs, so we return 5.

Example 2:

Input: nums = [1,2,3,4,5]
Output: 0
Explanation: There are no bad pairs.

Constraints:

  • 1 <= nums.length <= 10⁵
  • 1 <= nums[i] <= 10⁹

Test Cases

1
2
class Solution:
def countBadPairs(self, nums: List[int]) -> int:
solution_test.py
1
2
3
4
5
6
7
8
9
10
11
12
import pytest

from solution import Solution


@pytest.mark.parametrize('nums, expected', [
([4,1,3,3], 5),
([1,2,3,4,5], 0),
])
@pytest.mark.parametrize('sol', [Solution()])
def test_solution(sol, nums, expected):
assert sol.countBadPairs(nums) == expected

Thoughts

思路跟 1014. Best Sightseeing Pair 有点儿像。先把 j - i != nums[j] - nums[i] 改写成 nums[i] - i != nums[j] - j,把 i 和 j 的耦合打散。

然后为了计算不相等 pair 的数量,可以用总数减去相等的数量。

Pair 总数显然为 n * (n - 1) / 2。如果有 m 个位置的 nums[i] - i 彼此相等,那么他们贡献了 m * (m - 1) / 2 个相等的 pair。

用字典记录 nums[i] - i 的每个不同结果对应的 i 的数量,从总数中减去所有相等的 pair 数量,所得记为题目结果。

时间复杂度 O(n),空间复杂度 O(n)

Code

solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from collections import defaultdict


class Solution:
def countBadPairs(self, nums: list[int]) -> int:
counts: dict[int, int] = defaultdict(int) # nums[i] - i -> count
for i, num in enumerate(nums):
counts[num - i] += 1

n = len(nums)
total = n * (n - 1) // 2
for count in counts.values():
total -= count * (count - 1) // 2

return total