Skip to content

Commit e836c99

Browse files
authored
Merge branch 'doocs:main' into main
2 parents 090f7ec + 0d0ad7c commit e836c99

File tree

42 files changed

+897
-56
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+897
-56
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+
}

solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
comments: true
33
difficulty: 困难
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains/README.md
5+
tags:
6+
- 贪心
7+
- 数组
8+
- 数学
9+
- 堆(优先队列)
510
---
611

712
<!-- problem:start -->
@@ -24,9 +29,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3506.Fi
2429

2530
<ul>
2631
<li>第 <code>i</code> 个细菌菌株需要 <code>timeReq[i]</code> 个时间单位来被消除。</li>
27-
<li>单个白细胞只能消除 <strong>一种</strong> 细菌菌株。之后,白细胞耗尽,无法执行任何其他任务。</li>
32+
<li>单个白细胞只能消除 <strong>一个</strong> 细菌菌株。之后,白细胞耗尽,无法执行任何其他任务。</li>
2833
<li>一个白细胞可以将自身分裂为两个白细胞,但这需要&nbsp;<code>splitTime</code>&nbsp;单位时间。一旦分裂,两个白细胞就可以 <strong>并行</strong> 消灭细菌。</li>
29-
<li>仅有一个白细胞可以针对一个单一细菌菌株工作。多个白细胞不能同时攻击一个菌株。</li>
34+
<li>一个白细胞仅可以攻击一个细菌菌株。多个白细胞不能同时攻击一个菌株。</li>
3035
</ul>
3136

3237
<p>您必须确定消除所有细菌菌株所需的 <strong>最短</strong> 时间。</p>
@@ -58,7 +63,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3506.Fi
5863
<div class="example-block">
5964
<p><span class="example-io"><b>输入:</b>timeReq = [10,4], splitTime = 5</span></p>
6065

61-
<p><b>输出:</b>5</p>
66+
<p><b>输出:</b>15</p>
6267

6368
<p><strong>解释:</strong></p>
6469

solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README_EN.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
comments: true
33
difficulty: Hard
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains/README_EN.md
5+
tags:
6+
- Greedy
7+
- Array
8+
- Math
9+
- Heap (Priority Queue)
510
---
611

712
<!-- problem:start -->

solution/3500-3599/3507.Minimum Pair Removal to Sort Array I/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
comments: true
33
difficulty: 简单
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3507.Minimum%20Pair%20Removal%20to%20Sort%20Array%20I/README.md
5+
tags:
6+
- 数组
7+
- 哈希表
8+
- 链表
9+
- 双向链表
10+
- 有序集合
11+
- 模拟
12+
- 堆(优先队列)
513
---
614

715
<!-- problem:start -->

solution/3500-3599/3507.Minimum Pair Removal to Sort Array I/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
comments: true
33
difficulty: Easy
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3507.Minimum%20Pair%20Removal%20to%20Sort%20Array%20I/README_EN.md
5+
tags:
6+
- Array
7+
- Hash Table
8+
- Linked List
9+
- Doubly-Linked List
10+
- Ordered Set
11+
- Simulation
12+
- Heap (Priority Queue)
513
---
614

715
<!-- problem:start -->

solution/3500-3599/3508.Implement Router/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3508.Implement%20Router/README.md
5+
tags:
6+
- 设计
7+
- 队列
8+
- 数组
9+
- 哈希表
10+
- 二分查找
11+
- 有序集合
512
---
613

714
<!-- problem:start -->

solution/3500-3599/3508.Implement Router/README_EN.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3508.Implement%20Router/README_EN.md
5+
tags:
6+
- Design
7+
- Queue
8+
- Array
9+
- Hash Table
10+
- Binary Search
11+
- Ordered Set
512
---
613

714
<!-- problem:start -->

solution/3500-3599/3509.Maximum Product of Subsequences With an Alternating Sum Equal to K/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
comments: true
33
difficulty: 困难
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3509.Maximum%20Product%20of%20Subsequences%20With%20an%20Alternating%20Sum%20Equal%20to%20K/README.md
5+
tags:
6+
- 数组
7+
- 哈希表
8+
- 动态规划
59
---
610

711
<!-- problem:start -->

solution/3500-3599/3509.Maximum Product of Subsequences With an Alternating Sum Equal to K/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
comments: true
33
difficulty: Hard
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3509.Maximum%20Product%20of%20Subsequences%20With%20an%20Alternating%20Sum%20Equal%20to%20K/README_EN.md
5+
tags:
6+
- Array
7+
- Hash Table
8+
- Dynamic Programming
59
---
610

711
<!-- problem:start -->

solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README.md

+35-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: 简单
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3512.Minimum%20Operations%20to%20Make%20Array%20Sum%20Divisible%20by%20K/README.md
5+
tags:
6+
- 数组
7+
- 数学
58
---
69

710
<!-- problem:start -->
@@ -84,32 +87,60 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3512.Mi
8487

8588
<!-- solution:start -->
8689

87-
### 方法一
90+
### 方法一:求和取模
91+
92+
题目实际上是求数组元素之和对 $k$ 取模的结果。因此,我们只需要遍历数组,计算出所有元素之和,然后对 $k$ 取模,最后返回这个结果即可。
93+
94+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
8895

8996
<!-- tabs:start -->
9097

9198
#### Python3
9299

93100
```python
94-
101+
class Solution:
102+
def minOperations(self, nums: List[int], k: int) -> int:
103+
return sum(nums) % k
95104
```
96105

97106
#### Java
98107

99108
```java
100-
109+
class Solution {
110+
public int minOperations(int[] nums, int k) {
111+
return Arrays.stream(nums).sum() % k;
112+
}
113+
}
101114
```
102115

103116
#### C++
104117

105118
```cpp
106-
119+
class Solution {
120+
public:
121+
int minOperations(vector<int>& nums, int k) {
122+
return reduce(nums.begin(), nums.end(), 0) % k;
123+
}
124+
};
107125
```
108126
109127
#### Go
110128
111129
```go
130+
func minOperations(nums []int, k int) (ans int) {
131+
for _, x := range nums {
132+
ans = (ans + x) % k
133+
}
134+
return
135+
}
136+
```
137+
138+
#### TypeScript
112139

140+
```ts
141+
function minOperations(nums: number[], k: number): number {
142+
return nums.reduce((acc, x) => acc + x, 0) % k;
143+
}
113144
```
114145

115146
<!-- tabs:end -->

0 commit comments

Comments
 (0)