Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,50 @@ function maximumProcessableQueries(nums: number[], queries: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn maximum_processable_queries(nums: Vec<i32>, queries: Vec<i32>) -> 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,50 @@ function maximumProcessableQueries(nums: number[], queries: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn maximum_processable_queries(nums: Vec<i32>, queries: Vec<i32>) -> 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
impl Solution {
pub fn maximum_processable_queries(nums: Vec<i32>, queries: Vec<i32>) -> 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
}
}
20 changes: 19 additions & 1 deletion solution/3000-3099/3019.Number of Changing Keys/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tags:
<pre>
<strong>输入:</strong>s = "aAbBcC"
<strong>输出:</strong>2
<strong>解释:</strong>
<strong>解释:</strong>
从 s[0] = 'a' 到 s[1] = 'A',不存在按键变更,因为不计入 caps lock 或 shift 。
从 s[1] = 'A' 到 s[2] = 'b',按键变更。
从 s[2] = 'b' 到 s[3] = 'B',不存在按键变更,因为不计入 caps lock 或 shift 。
Expand Down Expand Up @@ -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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
20 changes: 19 additions & 1 deletion solution/3000-3099/3019.Number of Changing Keys/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ tags:
<pre>
<strong>Input:</strong> s = &quot;aAbBcC&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong>
<strong>Explanation:</strong>
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.
From s[1] = &#39;A&#39; to s[2] = &#39;b&#39;, there is a change of key.
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.
Expand Down Expand Up @@ -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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
13 changes: 13 additions & 0 deletions solution/3000-3099/3019.Number of Changing Keys/Solution.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,39 @@ function maximumLength(nums: number[]): number {
}
```

#### Rust

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

impl Solution {
pub fn maximum_length(nums: Vec<i32>) -> i32 {
let mut cnt: HashMap<i64, i32> = 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ tags:
<pre>
<strong>Input:</strong> nums = [1,3,2,4]
<strong>Output:</strong> 1
<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.
<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.
</pre>

<p>&nbsp;</p>
Expand Down Expand Up @@ -191,6 +191,39 @@ function maximumLength(nums: number[]): number {
}
```

#### Rust

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

impl Solution {
pub fn maximum_length(nums: Vec<i32>) -> i32 {
let mut cnt: HashMap<i64, i32> = 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::collections::HashMap;

impl Solution {
pub fn maximum_length(nums: Vec<i32>) -> i32 {
let mut cnt: HashMap<i64, i32> = 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
}
}