Skip to content

feat: add solutions to lc problem: No.2364 #4358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions solution/2300-2399/2364.Count Number of Bad Pairs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ tags:

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

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

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

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

最终,我们返回答案即可。
最后,我们返回答案即可。

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

<!-- tabs:start -->

Expand All @@ -97,8 +97,7 @@ class Solution {
long ans = 0;
for (int i = 0; i < nums.length; ++i) {
int x = i - nums[i];
ans += i - cnt.getOrDefault(x, 0);
cnt.merge(x, 1, Integer::sum);
ans += i - cnt.merge(x, 1, Integer::sum) + 1;
}
return ans;
}
Expand All @@ -115,8 +114,7 @@ public:
long long ans = 0;
for (int i = 0; i < nums.size(); ++i) {
int x = i - nums[i];
ans += i - cnt[x];
++cnt[x];
ans += i - cnt[x]++;
}
return ans;
}
Expand Down Expand Up @@ -152,6 +150,26 @@ function countBadPairs(nums: number[]): number {
}
```

#### Rust

```rust
use std::collections::HashMap;

impl Solution {
pub fn count_bad_pairs(nums: Vec<i32>) -> i64 {
let mut cnt: HashMap<i32, i64> = HashMap::new();
let mut ans: i64 = 0;
for (i, &num) in nums.iter().enumerate() {
let x = i as i32 - num;
let count = *cnt.get(&x).unwrap_or(&0);
ans += i as i64 - count;
*cnt.entry(x).or_insert(0) += 1;
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
34 changes: 26 additions & 8 deletions solution/2300-2399/2364.Count Number of Bad Pairs/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ There are a total of 5 bad pairs, so we return 5.

### Solution 1: Equation Transformation + Hash Table

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.
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.

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]$.
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]$.

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$.
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$.

Finally, we return the answer.

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

<!-- tabs:start -->

Expand All @@ -97,8 +97,7 @@ class Solution {
long ans = 0;
for (int i = 0; i < nums.length; ++i) {
int x = i - nums[i];
ans += i - cnt.getOrDefault(x, 0);
cnt.merge(x, 1, Integer::sum);
ans += i - cnt.merge(x, 1, Integer::sum) + 1;
}
return ans;
}
Expand All @@ -115,8 +114,7 @@ public:
long long ans = 0;
for (int i = 0; i < nums.size(); ++i) {
int x = i - nums[i];
ans += i - cnt[x];
++cnt[x];
ans += i - cnt[x]++;
}
return ans;
}
Expand Down Expand Up @@ -152,6 +150,26 @@ function countBadPairs(nums: number[]): number {
}
```

#### Rust

```rust
use std::collections::HashMap;

impl Solution {
pub fn count_bad_pairs(nums: Vec<i32>) -> i64 {
let mut cnt: HashMap<i32, i64> = HashMap::new();
let mut ans: i64 = 0;
for (i, &num) in nums.iter().enumerate() {
let x = i as i32 - num;
let count = *cnt.get(&x).unwrap_or(&0);
ans += i as i64 - count;
*cnt.entry(x).or_insert(0) += 1;
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ class Solution {
long long ans = 0;
for (int i = 0; i < nums.size(); ++i) {
int x = i - nums[i];
ans += i - cnt[x];
++cnt[x];
ans += i - cnt[x]++;
}
return ans;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ public long countBadPairs(int[] nums) {
long ans = 0;
for (int i = 0; i < nums.length; ++i) {
int x = i - nums[i];
ans += i - cnt.getOrDefault(x, 0);
cnt.merge(x, 1, Integer::sum);
ans += i - cnt.merge(x, 1, Integer::sum) + 1;
}
return ans;
}
Expand Down
15 changes: 15 additions & 0 deletions solution/2300-2399/2364.Count Number of Bad Pairs/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::collections::HashMap;

impl Solution {
pub fn count_bad_pairs(nums: Vec<i32>) -> i64 {
let mut cnt: HashMap<i32, i64> = HashMap::new();
let mut ans: i64 = 0;
for (i, &num) in nums.iter().enumerate() {
let x = i as i32 - num;
let count = *cnt.get(&x).unwrap_or(&0);
ans += i as i64 - count;
*cnt.entry(x).or_insert(0) += 1;
}
ans
}
}