LC 532. 数组中的 k-diff 数对

题目描述

这是 LeetCode 上的 532. 数组中的 k-diff 数对 ,难度为 中等

给定一个整数数组和一个整数 $k$,你需要在数组里找到 不同的 k-diff 数对,并返回不同的 k-diff 数对 的数目。

这里将 k-diff 数对定义为一个整数对 $(nums[i], nums[j])$,并满足下述全部条件:

  • 0 <= i < j < nums.length
  • |nums[i] - nums[j]| == k

注意,|val| 表示 val 的绝对值。

示例 1:

1
2
3
4
5
6
输入:nums = [3, 1, 4, 1, 5], k = 2

输出:2

解释:数组中有两个 2-diff 数对, (1, 3) 和 (3, 5)。
尽管数组中有两个1,但我们只应返回不同的数对的数量。

示例 2:
1
2
3
4
5
输入:nums = [1, 2, 3, 4, 5], k = 1

输出:4

解释:数组中有四个 1-diff 数对, (1, 2), (2, 3), (3, 4) 和 (4, 5)。

示例 3:
1
2
3
4
5
输入:nums = [1, 3, 1, 5, 4], k = 0

输出:1

解释:数组中只有一个 0-diff 数对,(1, 1)。

提示:

  • $1 <= nums.length <= 10^4$
  • $-10^7 <= nums[i] <= 10^7$
  • $0 <= k <= 10^7$

哈希表

一个简单的想法是先使用「哈希表」进行计数。

对于每个 $x = nums[i]$ 而言,根据 $k$ 是否为 $0$ 进行分情况讨论:

  • $k$ 为 $0$:此时只能 $(x, x)$ 组成数对,此时判断 $nums[i]$ 出现次数是否大于 $1$ 次,若满足则进行计数加一;
  • $k$ 不为 $0$:此时 $x$ 能够与 $a = x - k$ 或 $b = x + k$ 组成数对,分别查询 $a$ 和 $b$ 是否出现过,若出现过则进行计数加一。

为了防止相同的 $x$ 进行重复计数,我们需要统计完 $x$ 后,清空其出现次数。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int findPairs(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for (int i : nums) map.put(i, map.getOrDefault(i, 0) + 1);
int ans = 0;
for (int i : nums) {
if (map.get(i) == 0) continue;
if (k == 0) {
if (map.get(i) > 1) ans++;
} else {
int a = i - k, b = i + k;
if (map.getOrDefault(a, 0) > 0) ans++;
if (map.getOrDefault(b, 0) > 0) ans++;
}
map.put(i, 0);
}
return ans;
}
}

  • 时间复杂度:$O(n)$
  • 空间复杂度:$O(n)$

离散化 + 二分

我们知道可以使用「数组」充当「哈希表」来减少常数,但由于 $nums[i]$ 的值域大小为 $2 \times 10^7$,可能会有 MLE 风险。

我们需要对 $nums$ 进行离散化,预处理出一个去重且有序的数组 list

在统计答案时,我们不再需要在 nums 基础上统计,而可以直接在去重数组 list 上进行统计,同时将对「数值」的统计操作转换为对「下标」的统计操作。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
static int[] cnt = new int[10010];
List<Integer> list;
int find(int x) {
int n = list.size(), l = 0, r = n - 1;
while (l < r) {
int mid = l + r >> 1;
if (list.get(mid) >= x) r = mid;
else l = mid + 1;
}
return list.get(r) == x ? r : -1;
}
public int findPairs(int[] nums, int k) {
Arrays.sort(nums);
list = new ArrayList<>();
for (int i : nums) {
if (list.isEmpty() || i != list.get(list.size() - 1)) list.add(i);
}
Arrays.fill(cnt, 0);
for (int i : nums) cnt[find(i)]++;
int ans = 0, idx = 0;
for (int i : list) {
if (k == 0) {
if (cnt[idx] > 1) ans++;
} else {
int a = find(i - k), b = find(i + k);
if (a != -1 && cnt[a] > 0) ans++;
if (b != -1 && cnt[b] > 0) ans++;
}
cnt[idx++] = 0;
}
return ans;
}
}

  • 时间复杂度:排序离散化复杂度为 $O(n\log{n})$,统计答案复杂度为 $O(n\log{n})$。整体复杂度为 $O(n\log{n})$
  • 空间复杂度:$O(n)$

离散化 + 双指针

跟进一步的,对于 $x = nums[i]$ 而言,我们每次都在整段的 list 中二分找 $a = nums[i] - k$ 和 $b = nums[i] + k$ 的下标,导致我们统计 $nums[i]$ 对答案的贡献时复杂度为 $O(\log{n})$,统计所有 $nums[i]$ 对答案的贡献为 $O(n\log{n})$。

实际上,利用 list 本身的有序性,我们在从前往后处理每个 $nums[idx]$ 时,对应的 $a$ 和 $b$ 也必然是逐步增大,因此我们可以使用「双指针」来避免「二分」操作,使得统计所有 $nums[i]$ 对答案贡献的复杂度从 $O(n\log{n})$ 下降到 $O(n)$。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
static int[] cnt = new int[10010];
public int findPairs(int[] nums, int k) {
Arrays.sort(nums);
List<Integer> list = new ArrayList<>();
for (int i : nums) {
if (list.isEmpty() || i != list.get(list.size() - 1)) list.add(i);
}
Arrays.fill(cnt, 0);
for (int i = 0, j = 0; i < nums.length; i++) {
if (nums[i] != list.get(j)) j++;
cnt[j]++;
}
int n = list.size(), idx = 0, ans = 0, l = 0, r = 0;
for (int i : list) {
if (k == 0) {
if (cnt[idx] > 1) ans++;
} else {
int a = i - k, b = i + k;
while (l < n && list.get(l) < a) l++;
while (r < n && list.get(r) < b) r++;
if (l < n && list.get(l) == a && cnt[l] > 0) ans++;
if (r < n && list.get(r) == b && cnt[r] > 0) ans++;
}
cnt[idx++] = 0;
}
return ans;
}
}

  • 时间复杂度:排序离散化复杂度为 $O(n\log{n})$,统计答案复杂度为 $O(n)$,整体复杂度为 $O(n\log{n})$
  • 空间复杂度:$O(n)$

最后

这是我们「刷穿 LeetCode」系列文章的第 No.532 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。