Skip to content

Commit 6f8e0e5

Browse files
committed
feat: add solutions to lc problem: No.2364
No.2364.Count Number of Bad Pairs
1 parent 8e41e0d commit 6f8e0e5

File tree

5 files changed

+70
-21
lines changed

5 files changed

+70
-21
lines changed

solution/2300-2399/2364.Count Number of Bad Pairs/README.md

+27-9
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ tags:
6363

6464
### 方法一:式子转换 + 哈希表
6565

66-
根据题目描述,我们可以得知,对于任意的 $i \lt j$,如果 $j - i \neq nums[j] - nums[i]$,则 $(i, j)$ 是一个坏数对。
66+
根据题目描述,我们可以得知,对于任意的 $i \lt j$,如果 $j - i \neq \textit{nums}[j] - \textit{nums}[i]$,则 $(i, j)$ 是一个坏数对。
6767

68-
我们可以将式子转换为 $i - nums[i] \neq j - nums[j]$。这启发我们用哈希表 $cnt$ 来统计 $i - nums[i]$ 的出现次数。
68+
我们可以将式子转换为 $i - \textit{nums}[i] \neq j - \textit{nums}[j]$。这启发我们用哈希表 $cnt$ 来统计 $i - \textit{nums}[i]$ 的出现次数。
6969

70-
我们遍历数组,对于当前元素 $nums[i]$,我们将 $i - cnt[i - nums[i]]$ 加到答案中,然后将 $i - nums[i]$ 的出现次数加 $1$。
70+
遍历数组,对于当前元素 $\textit{nums}[i]$,我们将 $i - cnt[i - \textit{nums}[i]]$ 加到答案中,然后将 $i - \textit{nums}[i]$ 的出现次数加 $1$。
7171

72-
最终,我们返回答案即可。
72+
最后,我们返回答案即可。
7373

74-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组的长度
74+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums} 的长度
7575

7676
<!-- tabs:start -->
7777

@@ -97,8 +97,7 @@ class Solution {
9797
long ans = 0;
9898
for (int i = 0; i < nums.length; ++i) {
9999
int x = i - nums[i];
100-
ans += i - cnt.getOrDefault(x, 0);
101-
cnt.merge(x, 1, Integer::sum);
100+
ans += i - cnt.merge(x, 1, Integer::sum) + 1;
102101
}
103102
return ans;
104103
}
@@ -115,8 +114,7 @@ public:
115114
long long ans = 0;
116115
for (int i = 0; i < nums.size(); ++i) {
117116
int x = i - nums[i];
118-
ans += i - cnt[x];
119-
++cnt[x];
117+
ans += i - cnt[x]++;
120118
}
121119
return ans;
122120
}
@@ -152,6 +150,26 @@ function countBadPairs(nums: number[]): number {
152150
}
153151
```
154152

153+
#### Rust
154+
155+
```rust
156+
use std::collections::HashMap;
157+
158+
impl Solution {
159+
pub fn count_bad_pairs(nums: Vec<i32>) -> i64 {
160+
let mut cnt: HashMap<i32, i64> = HashMap::new();
161+
let mut ans: i64 = 0;
162+
for (i, &num) in nums.iter().enumerate() {
163+
let x = i as i32 - num;
164+
let count = *cnt.get(&x).unwrap_or(&0);
165+
ans += i as i64 - count;
166+
*cnt.entry(x).or_insert(0) += 1;
167+
}
168+
ans
169+
}
170+
}
171+
```
172+
155173
<!-- tabs:end -->
156174

157175
<!-- solution:end -->

solution/2300-2399/2364.Count Number of Bad Pairs/README_EN.md

+26-8
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ There are a total of 5 bad pairs, so we return 5.
6363

6464
### Solution 1: Equation Transformation + Hash Table
6565

66-
From the problem description, we know that for any $i < j$, if $j - i \neq nums[j] - nums[i]$, then $(i, j)$ is a bad pair.
66+
According to the problem description, for any $i \lt j$, if $j - i \neq \textit{nums}[j] - \textit{nums}[i]$, then $(i, j)$ is a bad pair.
6767

68-
We can transform the equation to $i - nums[i] \neq j - nums[j]$. This inspires us to use a hash table $cnt$ to count the occurrences of $i - nums[i]$.
68+
We can transform the equation into $i - \textit{nums}[i] \neq j - \textit{nums}[j]$. This suggests using a hash table $cnt$ to count the occurrences of $i - \textit{nums}[i]$.
6969

70-
We iterate through the array. For the current element $nums[i]$, we add $i - cnt[i - nums[i]]$ to the answer, then increment the count of $i - nums[i]$ by $1$.
70+
While iterating through the array, for the current element $\textit{nums}[i]$, we add $i - cnt[i - \textit{nums}[i]]$ to the answer, and then increment the count of $i - \textit{nums}[i]$ by $1$.
7171

7272
Finally, we return the answer.
7373

74-
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the array.
74+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$.
7575

7676
<!-- tabs:start -->
7777

@@ -97,8 +97,7 @@ class Solution {
9797
long ans = 0;
9898
for (int i = 0; i < nums.length; ++i) {
9999
int x = i - nums[i];
100-
ans += i - cnt.getOrDefault(x, 0);
101-
cnt.merge(x, 1, Integer::sum);
100+
ans += i - cnt.merge(x, 1, Integer::sum) + 1;
102101
}
103102
return ans;
104103
}
@@ -115,8 +114,7 @@ public:
115114
long long ans = 0;
116115
for (int i = 0; i < nums.size(); ++i) {
117116
int x = i - nums[i];
118-
ans += i - cnt[x];
119-
++cnt[x];
117+
ans += i - cnt[x]++;
120118
}
121119
return ans;
122120
}
@@ -152,6 +150,26 @@ function countBadPairs(nums: number[]): number {
152150
}
153151
```
154152

153+
#### Rust
154+
155+
```rust
156+
use std::collections::HashMap;
157+
158+
impl Solution {
159+
pub fn count_bad_pairs(nums: Vec<i32>) -> i64 {
160+
let mut cnt: HashMap<i32, i64> = HashMap::new();
161+
let mut ans: i64 = 0;
162+
for (i, &num) in nums.iter().enumerate() {
163+
let x = i as i32 - num;
164+
let count = *cnt.get(&x).unwrap_or(&0);
165+
ans += i as i64 - count;
166+
*cnt.entry(x).or_insert(0) += 1;
167+
}
168+
ans
169+
}
170+
}
171+
```
172+
155173
<!-- tabs:end -->
156174

157175
<!-- solution:end -->

solution/2300-2399/2364.Count Number of Bad Pairs/Solution.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ class Solution {
55
long long ans = 0;
66
for (int i = 0; i < nums.size(); ++i) {
77
int x = i - nums[i];
8-
ans += i - cnt[x];
9-
++cnt[x];
8+
ans += i - cnt[x]++;
109
}
1110
return ans;
1211
}

solution/2300-2399/2364.Count Number of Bad Pairs/Solution.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ public long countBadPairs(int[] nums) {
44
long ans = 0;
55
for (int i = 0; i < nums.length; ++i) {
66
int x = i - nums[i];
7-
ans += i - cnt.getOrDefault(x, 0);
8-
cnt.merge(x, 1, Integer::sum);
7+
ans += i - cnt.merge(x, 1, Integer::sum) + 1;
98
}
109
return ans;
1110
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn count_bad_pairs(nums: Vec<i32>) -> i64 {
5+
let mut cnt: HashMap<i32, i64> = HashMap::new();
6+
let mut ans: i64 = 0;
7+
for (i, &num) in nums.iter().enumerate() {
8+
let x = i as i32 - num;
9+
let count = *cnt.get(&x).unwrap_or(&0);
10+
ans += i as i64 - count;
11+
*cnt.entry(x).or_insert(0) += 1;
12+
}
13+
ans
14+
}
15+
}

0 commit comments

Comments
 (0)