Skip to content

Commit 7c89a76

Browse files
authored
feat: add solutions to lc problems: No.3018~3020 (#4675)
1 parent 247a319 commit 7c89a76

File tree

9 files changed

+275
-3
lines changed

9 files changed

+275
-3
lines changed

solution/3000-3099/3018.Maximum Number of Removal Queries That Can Be Processed I/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,50 @@ function maximumProcessableQueries(nums: number[], queries: number[]): number {
273273
}
274274
```
275275

276+
#### Rust
277+
278+
```rust
279+
impl Solution {
280+
pub fn maximum_processable_queries(nums: Vec<i32>, queries: Vec<i32>) -> i32 {
281+
let n = nums.len();
282+
let m = queries.len();
283+
let mut f = vec![vec![0; n]; n];
284+
285+
for i in 0..n {
286+
for j in (i..n).rev() {
287+
if i > 0 {
288+
let idx = f[i - 1][j] as usize;
289+
if idx < m {
290+
f[i][j] = f[i][j].max(f[i - 1][j] + if nums[i - 1] >= queries[idx] { 1 } else { 0 });
291+
}
292+
}
293+
if j + 1 < n {
294+
let idx = f[i][j + 1] as usize;
295+
if idx < m {
296+
f[i][j] = f[i][j].max(f[i][j + 1] + if nums[j + 1] >= queries[idx] { 1 } else { 0 });
297+
}
298+
}
299+
if f[i][j] as usize == m {
300+
return m as i32;
301+
}
302+
}
303+
}
304+
305+
let mut ans = 0;
306+
for i in 0..n {
307+
let idx = f[i][i] as usize;
308+
if idx < m {
309+
ans = ans.max(f[i][i] + if nums[i] >= queries[idx] { 1 } else { 0 });
310+
} else {
311+
ans = ans.max(f[i][i]);
312+
}
313+
}
314+
315+
ans
316+
}
317+
}
318+
```
319+
276320
<!-- tabs:end -->
277321

278322
<!-- solution:end -->

solution/3000-3099/3018.Maximum Number of Removal Queries That Can Be Processed I/README_EN.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,50 @@ function maximumProcessableQueries(nums: number[], queries: number[]): number {
271271
}
272272
```
273273

274+
#### Rust
275+
276+
```rust
277+
impl Solution {
278+
pub fn maximum_processable_queries(nums: Vec<i32>, queries: Vec<i32>) -> i32 {
279+
let n = nums.len();
280+
let m = queries.len();
281+
let mut f = vec![vec![0; n]; n];
282+
283+
for i in 0..n {
284+
for j in (i..n).rev() {
285+
if i > 0 {
286+
let idx = f[i - 1][j] as usize;
287+
if idx < m {
288+
f[i][j] = f[i][j].max(f[i - 1][j] + if nums[i - 1] >= queries[idx] { 1 } else { 0 });
289+
}
290+
}
291+
if j + 1 < n {
292+
let idx = f[i][j + 1] as usize;
293+
if idx < m {
294+
f[i][j] = f[i][j].max(f[i][j + 1] + if nums[j + 1] >= queries[idx] { 1 } else { 0 });
295+
}
296+
}
297+
if f[i][j] as usize == m {
298+
return m as i32;
299+
}
300+
}
301+
}
302+
303+
let mut ans = 0;
304+
for i in 0..n {
305+
let idx = f[i][i] as usize;
306+
if idx < m {
307+
ans = ans.max(f[i][i] + if nums[i] >= queries[idx] { 1 } else { 0 });
308+
} else {
309+
ans = ans.max(f[i][i]);
310+
}
311+
}
312+
313+
ans
314+
}
315+
}
316+
```
317+
274318
<!-- tabs:end -->
275319

276320
<!-- solution:end -->
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
impl Solution {
2+
pub fn maximum_processable_queries(nums: Vec<i32>, queries: Vec<i32>) -> i32 {
3+
let n = nums.len();
4+
let m = queries.len();
5+
let mut f = vec![vec![0; n]; n];
6+
7+
for i in 0..n {
8+
for j in (i..n).rev() {
9+
if i > 0 {
10+
let idx = f[i - 1][j] as usize;
11+
if idx < m {
12+
f[i][j] = f[i][j]
13+
.max(f[i - 1][j] + if nums[i - 1] >= queries[idx] { 1 } else { 0 });
14+
}
15+
}
16+
if j + 1 < n {
17+
let idx = f[i][j + 1] as usize;
18+
if idx < m {
19+
f[i][j] = f[i][j]
20+
.max(f[i][j + 1] + if nums[j + 1] >= queries[idx] { 1 } else { 0 });
21+
}
22+
}
23+
if f[i][j] as usize == m {
24+
return m as i32;
25+
}
26+
}
27+
}
28+
29+
let mut ans = 0;
30+
for i in 0..n {
31+
let idx = f[i][i] as usize;
32+
if idx < m {
33+
ans = ans.max(f[i][i] + if nums[i] >= queries[idx] { 1 } else { 0 });
34+
} else {
35+
ans = ans.max(f[i][i]);
36+
}
37+
}
38+
39+
ans
40+
}
41+
}

solution/3000-3099/3019.Number of Changing Keys/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ tags:
3131
<pre>
3232
<strong>输入:</strong>s = "aAbBcC"
3333
<strong>输出:</strong>2
34-
<strong>解释:</strong>
34+
<strong>解释:</strong>
3535
从 s[0] = 'a' 到 s[1] = 'A',不存在按键变更,因为不计入 caps lock 或 shift 。
3636
从 s[1] = 'A' 到 s[2] = 'b',按键变更。
3737
从 s[2] = 'b' 到 s[3] = 'B',不存在按键变更,因为不计入 caps lock 或 shift 。
@@ -139,6 +139,24 @@ function countKeyChanges(s: string): number {
139139
}
140140
```
141141

142+
#### Rust
143+
144+
```rust
145+
impl Solution {
146+
pub fn count_key_changes(s: String) -> i32 {
147+
let s = s.to_lowercase();
148+
let bytes = s.as_bytes();
149+
let mut ans = 0;
150+
for i in 1..bytes.len() {
151+
if bytes[i] != bytes[i - 1] {
152+
ans += 1;
153+
}
154+
}
155+
ans
156+
}
157+
}
158+
```
159+
142160
<!-- tabs:end -->
143161

144162
<!-- solution:end -->

solution/3000-3099/3019.Number of Changing Keys/README_EN.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ tags:
3030
<pre>
3131
<strong>Input:</strong> s = &quot;aAbBcC&quot;
3232
<strong>Output:</strong> 2
33-
<strong>Explanation:</strong>
33+
<strong>Explanation:</strong>
3434
From s[0] = &#39;a&#39; to s[1] = &#39;A&#39;, there is no change of key as caps lock or shift is not counted.
3535
From s[1] = &#39;A&#39; to s[2] = &#39;b&#39;, there is a change of key.
3636
From s[2] = &#39;b&#39; to s[3] = &#39;B&#39;, there is no change of key as caps lock or shift is not counted.
@@ -138,6 +138,24 @@ function countKeyChanges(s: string): number {
138138
}
139139
```
140140

141+
#### Rust
142+
143+
```rust
144+
impl Solution {
145+
pub fn count_key_changes(s: String) -> i32 {
146+
let s = s.to_lowercase();
147+
let bytes = s.as_bytes();
148+
let mut ans = 0;
149+
for i in 1..bytes.len() {
150+
if bytes[i] != bytes[i - 1] {
151+
ans += 1;
152+
}
153+
}
154+
ans
155+
}
156+
}
157+
```
158+
141159
<!-- tabs:end -->
142160

143161
<!-- solution:end -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn count_key_changes(s: String) -> i32 {
3+
let s = s.to_lowercase();
4+
let bytes = s.as_bytes();
5+
let mut ans = 0;
6+
for i in 1..bytes.len() {
7+
if bytes[i] != bytes[i - 1] {
8+
ans += 1;
9+
}
10+
}
11+
ans
12+
}
13+
}

solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,39 @@ function maximumLength(nums: number[]): number {
193193
}
194194
```
195195

196+
#### Rust
197+
198+
```rust
199+
use std::collections::HashMap;
200+
201+
impl Solution {
202+
pub fn maximum_length(nums: Vec<i32>) -> i32 {
203+
let mut cnt: HashMap<i64, i32> = HashMap::new();
204+
for &x in &nums {
205+
*cnt.entry(x as i64).or_insert(0) += 1;
206+
}
207+
208+
let mut ans = 0;
209+
if let Some(t) = cnt.remove(&1) {
210+
ans = t - ((t % 2) ^ 1);
211+
}
212+
213+
for &key in cnt.keys() {
214+
let mut x = key;
215+
let mut t = 0;
216+
while *cnt.get(&x).unwrap_or(&0) > 1 {
217+
x = x * x;
218+
t += 2;
219+
}
220+
t += cnt.get(&x).unwrap_or(&-1);
221+
ans = ans.max(t);
222+
}
223+
224+
ans
225+
}
226+
}
227+
```
228+
196229
<!-- tabs:end -->
197230

198231
<!-- solution:end -->

solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README_EN.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ tags:
4444
<pre>
4545
<strong>Input:</strong> nums = [1,3,2,4]
4646
<strong>Output:</strong> 1
47-
<strong>Explanation:</strong> We can select the subset {1}, which can be placed in the array as [1] which follows the pattern. Hence the answer is 1. Note that we could have also selected the subsets {2}, {3}, or {4}, there may be multiple subsets which provide the same answer.
47+
<strong>Explanation:</strong> We can select the subset {1}, which can be placed in the array as [1] which follows the pattern. Hence the answer is 1. Note that we could have also selected the subsets {2}, {3}, or {4}, there may be multiple subsets which provide the same answer.
4848
</pre>
4949

5050
<p>&nbsp;</p>
@@ -191,6 +191,39 @@ function maximumLength(nums: number[]): number {
191191
}
192192
```
193193

194+
#### Rust
195+
196+
```rust
197+
use std::collections::HashMap;
198+
199+
impl Solution {
200+
pub fn maximum_length(nums: Vec<i32>) -> i32 {
201+
let mut cnt: HashMap<i64, i32> = HashMap::new();
202+
for &x in &nums {
203+
*cnt.entry(x as i64).or_insert(0) += 1;
204+
}
205+
206+
let mut ans = 0;
207+
if let Some(t) = cnt.remove(&1) {
208+
ans = t - ((t % 2) ^ 1);
209+
}
210+
211+
for &key in cnt.keys() {
212+
let mut x = key;
213+
let mut t = 0;
214+
while *cnt.get(&x).unwrap_or(&0) > 1 {
215+
x = x * x;
216+
t += 2;
217+
}
218+
t += cnt.get(&x).unwrap_or(&-1);
219+
ans = ans.max(t);
220+
}
221+
222+
ans
223+
}
224+
}
225+
```
226+
194227
<!-- tabs:end -->
195228

196229
<!-- solution:end -->
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn maximum_length(nums: Vec<i32>) -> i32 {
5+
let mut cnt: HashMap<i64, i32> = HashMap::new();
6+
for &x in &nums {
7+
*cnt.entry(x as i64).or_insert(0) += 1;
8+
}
9+
10+
let mut ans = 0;
11+
if let Some(t) = cnt.remove(&1) {
12+
ans = t - ((t % 2) ^ 1);
13+
}
14+
15+
for &key in cnt.keys() {
16+
let mut x = key;
17+
let mut t = 0;
18+
while *cnt.get(&x).unwrap_or(&0) > 1 {
19+
x = x * x;
20+
t += 2;
21+
}
22+
t += cnt.get(&x).unwrap_or(&-1);
23+
ans = ans.max(t);
24+
}
25+
26+
ans
27+
}
28+
}

0 commit comments

Comments
 (0)