diff --git a/solution/3000-3099/3018.Maximum Number of Removal Queries That Can Be Processed I/README.md b/solution/3000-3099/3018.Maximum Number of Removal Queries That Can Be Processed I/README.md index f052a78651502..757c5fb2b041e 100644 --- a/solution/3000-3099/3018.Maximum Number of Removal Queries That Can Be Processed I/README.md +++ b/solution/3000-3099/3018.Maximum Number of Removal Queries That Can Be Processed I/README.md @@ -273,6 +273,50 @@ function maximumProcessableQueries(nums: number[], queries: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn maximum_processable_queries(nums: Vec, queries: Vec) -> i32 { + let n = nums.len(); + let m = queries.len(); + let mut f = vec![vec![0; n]; n]; + + for i in 0..n { + for j in (i..n).rev() { + if i > 0 { + let idx = f[i - 1][j] as usize; + if idx < m { + f[i][j] = f[i][j].max(f[i - 1][j] + if nums[i - 1] >= queries[idx] { 1 } else { 0 }); + } + } + if j + 1 < n { + let idx = f[i][j + 1] as usize; + if idx < m { + f[i][j] = f[i][j].max(f[i][j + 1] + if nums[j + 1] >= queries[idx] { 1 } else { 0 }); + } + } + if f[i][j] as usize == m { + return m as i32; + } + } + } + + let mut ans = 0; + for i in 0..n { + let idx = f[i][i] as usize; + if idx < m { + ans = ans.max(f[i][i] + if nums[i] >= queries[idx] { 1 } else { 0 }); + } else { + ans = ans.max(f[i][i]); + } + } + + ans + } +} +``` + diff --git a/solution/3000-3099/3018.Maximum Number of Removal Queries That Can Be Processed I/README_EN.md b/solution/3000-3099/3018.Maximum Number of Removal Queries That Can Be Processed I/README_EN.md index 31ee5f85dfc83..f037061264cb3 100644 --- a/solution/3000-3099/3018.Maximum Number of Removal Queries That Can Be Processed I/README_EN.md +++ b/solution/3000-3099/3018.Maximum Number of Removal Queries That Can Be Processed I/README_EN.md @@ -271,6 +271,50 @@ function maximumProcessableQueries(nums: number[], queries: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn maximum_processable_queries(nums: Vec, queries: Vec) -> i32 { + let n = nums.len(); + let m = queries.len(); + let mut f = vec![vec![0; n]; n]; + + for i in 0..n { + for j in (i..n).rev() { + if i > 0 { + let idx = f[i - 1][j] as usize; + if idx < m { + f[i][j] = f[i][j].max(f[i - 1][j] + if nums[i - 1] >= queries[idx] { 1 } else { 0 }); + } + } + if j + 1 < n { + let idx = f[i][j + 1] as usize; + if idx < m { + f[i][j] = f[i][j].max(f[i][j + 1] + if nums[j + 1] >= queries[idx] { 1 } else { 0 }); + } + } + if f[i][j] as usize == m { + return m as i32; + } + } + } + + let mut ans = 0; + for i in 0..n { + let idx = f[i][i] as usize; + if idx < m { + ans = ans.max(f[i][i] + if nums[i] >= queries[idx] { 1 } else { 0 }); + } else { + ans = ans.max(f[i][i]); + } + } + + ans + } +} +``` + diff --git a/solution/3000-3099/3018.Maximum Number of Removal Queries That Can Be Processed I/Solution.rs b/solution/3000-3099/3018.Maximum Number of Removal Queries That Can Be Processed I/Solution.rs new file mode 100644 index 0000000000000..92fff0a13949f --- /dev/null +++ b/solution/3000-3099/3018.Maximum Number of Removal Queries That Can Be Processed I/Solution.rs @@ -0,0 +1,41 @@ +impl Solution { + pub fn maximum_processable_queries(nums: Vec, queries: Vec) -> i32 { + let n = nums.len(); + let m = queries.len(); + let mut f = vec![vec![0; n]; n]; + + for i in 0..n { + for j in (i..n).rev() { + if i > 0 { + let idx = f[i - 1][j] as usize; + if idx < m { + f[i][j] = f[i][j] + .max(f[i - 1][j] + if nums[i - 1] >= queries[idx] { 1 } else { 0 }); + } + } + if j + 1 < n { + let idx = f[i][j + 1] as usize; + if idx < m { + f[i][j] = f[i][j] + .max(f[i][j + 1] + if nums[j + 1] >= queries[idx] { 1 } else { 0 }); + } + } + if f[i][j] as usize == m { + return m as i32; + } + } + } + + let mut ans = 0; + for i in 0..n { + let idx = f[i][i] as usize; + if idx < m { + ans = ans.max(f[i][i] + if nums[i] >= queries[idx] { 1 } else { 0 }); + } else { + ans = ans.max(f[i][i]); + } + } + + ans + } +} diff --git a/solution/3000-3099/3019.Number of Changing Keys/README.md b/solution/3000-3099/3019.Number of Changing Keys/README.md index f4185f581cf1f..cebe4b0bf034c 100644 --- a/solution/3000-3099/3019.Number of Changing Keys/README.md +++ b/solution/3000-3099/3019.Number of Changing Keys/README.md @@ -31,7 +31,7 @@ tags:
 输入:s = "aAbBcC"
 输出:2
-解释: 
+解释:
 从 s[0] = 'a' 到 s[1] = 'A',不存在按键变更,因为不计入 caps lock 或 shift 。
 从 s[1] = 'A' 到 s[2] = 'b',按键变更。
 从 s[2] = 'b' 到 s[3] = 'B',不存在按键变更,因为不计入 caps lock 或 shift 。
@@ -139,6 +139,24 @@ function countKeyChanges(s: string): number {
 }
 ```
 
+#### Rust
+
+```rust
+impl Solution {
+    pub fn count_key_changes(s: String) -> i32 {
+        let s = s.to_lowercase();
+        let bytes = s.as_bytes();
+        let mut ans = 0;
+        for i in 1..bytes.len() {
+            if bytes[i] != bytes[i - 1] {
+                ans += 1;
+            }
+        }
+        ans
+    }
+}
+```
+
 
 
 
diff --git a/solution/3000-3099/3019.Number of Changing Keys/README_EN.md b/solution/3000-3099/3019.Number of Changing Keys/README_EN.md
index dab3135859c5e..e37be3d59b7c5 100644
--- a/solution/3000-3099/3019.Number of Changing Keys/README_EN.md	
+++ b/solution/3000-3099/3019.Number of Changing Keys/README_EN.md	
@@ -30,7 +30,7 @@ tags:
 
 Input: s = "aAbBcC"
 Output: 2
-Explanation: 
+Explanation:
 From s[0] = 'a' to s[1] = 'A', there is no change of key as caps lock or shift is not counted.
 From s[1] = 'A' to s[2] = 'b', there is a change of key.
 From s[2] = 'b' to s[3] = 'B', there is no change of key as caps lock or shift is not counted.
@@ -138,6 +138,24 @@ function countKeyChanges(s: string): number {
 }
 ```
 
+#### Rust
+
+```rust
+impl Solution {
+    pub fn count_key_changes(s: String) -> i32 {
+        let s = s.to_lowercase();
+        let bytes = s.as_bytes();
+        let mut ans = 0;
+        for i in 1..bytes.len() {
+            if bytes[i] != bytes[i - 1] {
+                ans += 1;
+            }
+        }
+        ans
+    }
+}
+```
+
 
 
 
diff --git a/solution/3000-3099/3019.Number of Changing Keys/Solution.rs b/solution/3000-3099/3019.Number of Changing Keys/Solution.rs
new file mode 100644
index 0000000000000..a5aca5e010d8e
--- /dev/null
+++ b/solution/3000-3099/3019.Number of Changing Keys/Solution.rs	
@@ -0,0 +1,13 @@
+impl Solution {
+    pub fn count_key_changes(s: String) -> i32 {
+        let s = s.to_lowercase();
+        let bytes = s.as_bytes();
+        let mut ans = 0;
+        for i in 1..bytes.len() {
+            if bytes[i] != bytes[i - 1] {
+                ans += 1;
+            }
+        }
+        ans
+    }
+}
diff --git a/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README.md b/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README.md
index c801f8390d059..710e5ebd4a54a 100644
--- a/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README.md	
+++ b/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README.md	
@@ -193,6 +193,39 @@ function maximumLength(nums: number[]): number {
 }
 ```
 
+#### Rust
+
+```rust
+use std::collections::HashMap;
+
+impl Solution {
+    pub fn maximum_length(nums: Vec) -> i32 {
+        let mut cnt: HashMap = HashMap::new();
+        for &x in &nums {
+            *cnt.entry(x as i64).or_insert(0) += 1;
+        }
+
+        let mut ans = 0;
+        if let Some(t) = cnt.remove(&1) {
+            ans = t - ((t % 2) ^ 1);
+        }
+
+        for &key in cnt.keys() {
+            let mut x = key;
+            let mut t = 0;
+            while *cnt.get(&x).unwrap_or(&0) > 1 {
+                x = x * x;
+                t += 2;
+            }
+            t += cnt.get(&x).unwrap_or(&-1);
+            ans = ans.max(t);
+        }
+
+        ans
+    }
+}
+```
+
 
 
 
diff --git a/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README_EN.md b/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README_EN.md
index 664e9a347794d..12c1e03f62eb6 100644
--- a/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README_EN.md	
+++ b/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README_EN.md	
@@ -44,7 +44,7 @@ tags:
 
 Input: nums = [1,3,2,4]
 Output: 1
-Explanation: 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. 
+Explanation: 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.
 

 

@@ -191,6 +191,39 @@ function maximumLength(nums: number[]): number { } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn maximum_length(nums: Vec) -> i32 { + let mut cnt: HashMap = HashMap::new(); + for &x in &nums { + *cnt.entry(x as i64).or_insert(0) += 1; + } + + let mut ans = 0; + if let Some(t) = cnt.remove(&1) { + ans = t - ((t % 2) ^ 1); + } + + for &key in cnt.keys() { + let mut x = key; + let mut t = 0; + while *cnt.get(&x).unwrap_or(&0) > 1 { + x = x * x; + t += 2; + } + t += cnt.get(&x).unwrap_or(&-1); + ans = ans.max(t); + } + + ans + } +} +``` + diff --git a/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/Solution.rs b/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/Solution.rs new file mode 100644 index 0000000000000..b00a0fcbcc091 --- /dev/null +++ b/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/Solution.rs @@ -0,0 +1,28 @@ +use std::collections::HashMap; + +impl Solution { + pub fn maximum_length(nums: Vec) -> i32 { + let mut cnt: HashMap = HashMap::new(); + for &x in &nums { + *cnt.entry(x as i64).or_insert(0) += 1; + } + + let mut ans = 0; + if let Some(t) = cnt.remove(&1) { + ans = t - ((t % 2) ^ 1); + } + + for &key in cnt.keys() { + let mut x = key; + let mut t = 0; + while *cnt.get(&x).unwrap_or(&0) > 1 { + x = x * x; + t += 2; + } + t += cnt.get(&x).unwrap_or(&-1); + ans = ans.max(t); + } + + ans + } +}