diff --git a/solution/2300-2399/2364.Count Number of Bad Pairs/README.md b/solution/2300-2399/2364.Count Number of Bad Pairs/README.md index a201630d8b0d2..f8819a25b7090 100644 --- a/solution/2300-2399/2364.Count Number of Bad Pairs/README.md +++ b/solution/2300-2399/2364.Count Number of Bad Pairs/README.md @@ -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}$ 的长度。 @@ -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; } @@ -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; } @@ -152,6 +150,26 @@ function countBadPairs(nums: number[]): number { } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn count_bad_pairs(nums: Vec) -> i64 { + let mut cnt: HashMap = 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 + } +} +``` + diff --git a/solution/2300-2399/2364.Count Number of Bad Pairs/README_EN.md b/solution/2300-2399/2364.Count Number of Bad Pairs/README_EN.md index 4f0c61f0b143f..681122d4c0b07 100644 --- a/solution/2300-2399/2364.Count Number of Bad Pairs/README_EN.md +++ b/solution/2300-2399/2364.Count Number of Bad Pairs/README_EN.md @@ -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}$. @@ -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; } @@ -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; } @@ -152,6 +150,26 @@ function countBadPairs(nums: number[]): number { } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn count_bad_pairs(nums: Vec) -> i64 { + let mut cnt: HashMap = 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 + } +} +``` + diff --git a/solution/2300-2399/2364.Count Number of Bad Pairs/Solution.cpp b/solution/2300-2399/2364.Count Number of Bad Pairs/Solution.cpp index 61b7afa62c470..5eaae69370aed 100644 --- a/solution/2300-2399/2364.Count Number of Bad Pairs/Solution.cpp +++ b/solution/2300-2399/2364.Count Number of Bad Pairs/Solution.cpp @@ -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; } diff --git a/solution/2300-2399/2364.Count Number of Bad Pairs/Solution.java b/solution/2300-2399/2364.Count Number of Bad Pairs/Solution.java index 61ddfa4015659..1443a2c529248 100644 --- a/solution/2300-2399/2364.Count Number of Bad Pairs/Solution.java +++ b/solution/2300-2399/2364.Count Number of Bad Pairs/Solution.java @@ -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; } diff --git a/solution/2300-2399/2364.Count Number of Bad Pairs/Solution.rs b/solution/2300-2399/2364.Count Number of Bad Pairs/Solution.rs new file mode 100644 index 0000000000000..4cbfc4eb05fca --- /dev/null +++ b/solution/2300-2399/2364.Count Number of Bad Pairs/Solution.rs @@ -0,0 +1,15 @@ +use std::collections::HashMap; + +impl Solution { + pub fn count_bad_pairs(nums: Vec) -> i64 { + let mut cnt: HashMap = 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 + } +}