Skip to content

feat: add rust solutions to lc problems: No.3573,3577,3578 #4484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2025
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 @@ -254,6 +254,32 @@ function maximumProfit(prices: number[], k: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn maximum_profit(prices: Vec<i32>, k: i32) -> i64 {
let n = prices.len();
let k = k as usize;
let mut f = vec![vec![vec![0i64; 3]; k + 1]; n];
for j in 1..=k {
f[0][j][1] = -(prices[0] as i64);
f[0][j][2] = prices[0] as i64;
}
for i in 1..n {
for j in 1..=k {
f[i][j][0] = f[i - 1][j][0]
.max(f[i - 1][j][1] + prices[i] as i64)
.max(f[i - 1][j][2] - prices[i] as i64);
f[i][j][1] = f[i - 1][j][1].max(f[i - 1][j - 1][0] - prices[i] as i64);
f[i][j][2] = f[i - 1][j][2].max(f[i - 1][j - 1][0] + prices[i] as i64);
}
}
f[n - 1][k][0]
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,32 @@ function maximumProfit(prices: number[], k: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn maximum_profit(prices: Vec<i32>, k: i32) -> i64 {
let n = prices.len();
let k = k as usize;
let mut f = vec![vec![vec![0i64; 3]; k + 1]; n];
for j in 1..=k {
f[0][j][1] = -(prices[0] as i64);
f[0][j][2] = prices[0] as i64;
}
for i in 1..n {
for j in 1..=k {
f[i][j][0] = f[i - 1][j][0]
.max(f[i - 1][j][1] + prices[i] as i64)
.max(f[i - 1][j][2] - prices[i] as i64);
f[i][j][1] = f[i - 1][j][1].max(f[i - 1][j - 1][0] - prices[i] as i64);
f[i][j][2] = f[i - 1][j][2].max(f[i - 1][j - 1][0] + prices[i] as i64);
}
}
f[n - 1][k][0]
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
impl Solution {
pub fn maximum_profit(prices: Vec<i32>, k: i32) -> i64 {
let n = prices.len();
let k = k as usize;
let mut f = vec![vec![vec![0i64; 3]; k + 1]; n];
for j in 1..=k {
f[0][j][1] = -(prices[0] as i64);
f[0][j][2] = prices[0] as i64;
}
for i in 1..n {
for j in 1..=k {
f[i][j][0] = f[i - 1][j][0]
.max(f[i - 1][j][1] + prices[i] as i64)
.max(f[i - 1][j][2] - prices[i] as i64);
f[i][j][1] = f[i - 1][j][1].max(f[i - 1][j - 1][0] - prices[i] as i64);
f[i][j][2] = f[i - 1][j][2].max(f[i - 1][j - 1][0] + prices[i] as i64);
}
}
f[n - 1][k][0]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,31 @@ function canMakeEqual(nums: number[], k: number): boolean {
}
```

#### Rust

```rust
impl Solution {
pub fn can_make_equal(nums: Vec<i32>, k: i32) -> bool {
fn check(target: i32, k: i32, nums: &Vec<i32>) -> bool {
let mut cnt = 0;
let mut sign = 1;
for i in 0..nums.len() - 1 {
let x = nums[i] * sign;
if x == target {
sign = 1;
} else {
sign = -1;
cnt += 1;
}
}
cnt <= k && nums[nums.len() - 1] * sign == target
}

check(nums[0], k, &nums) || check(-nums[0], k, &nums)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,31 @@ function canMakeEqual(nums: number[], k: number): boolean {
}
```

#### Rust

```rust
impl Solution {
pub fn can_make_equal(nums: Vec<i32>, k: i32) -> bool {
fn check(target: i32, k: i32, nums: &Vec<i32>) -> bool {
let mut cnt = 0;
let mut sign = 1;
for i in 0..nums.len() - 1 {
let x = nums[i] * sign;
if x == target {
sign = 1;
} else {
sign = -1;
cnt += 1;
}
}
cnt <= k && nums[nums.len() - 1] * sign == target
}

check(nums[0], k, &nums) || check(-nums[0], k, &nums)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
impl Solution {
pub fn can_make_equal(nums: Vec<i32>, k: i32) -> bool {
fn check(target: i32, k: i32, nums: &Vec<i32>) -> bool {
let mut cnt = 0;
let mut sign = 1;
for i in 0..nums.len() - 1 {
let x = nums[i] * sign;
if x == target {
sign = 1;
} else {
sign = -1;
cnt += 1;
}
}
cnt <= k && nums[nums.len() - 1] * sign == target
}

check(nums[0], k, &nums) || check(-nums[0], k, &nums)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,24 @@ function countPermutations(complexity: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn count_permutations(complexity: Vec<i32>) -> i32 {
const MOD: i64 = 1_000_000_007;
let mut ans = 1i64;
for i in 1..complexity.len() {
if complexity[i] <= complexity[0] {
return 0;
}
ans = ans * i as i64 % MOD;
}
ans as i32
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,24 @@ function countPermutations(complexity: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn count_permutations(complexity: Vec<i32>) -> i32 {
const MOD: i64 = 1_000_000_007;
let mut ans = 1i64;
for i in 1..complexity.len() {
if complexity[i] <= complexity[0] {
return 0;
}
ans = ans * i as i64 % MOD;
}
ans as i32
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
impl Solution {
pub fn count_permutations(complexity: Vec<i32>) -> i32 {
const MOD: i64 = 1_000_000_007;
let mut ans = 1i64;
for i in 1..complexity.len() {
if complexity[i] <= complexity[0] {
return 0;
}
ans = ans * i as i64 % MOD;
}
ans as i32
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,42 @@ class TreapMultiSet<T = number> implements ITreapMultiSet<T> {
}
```

#### Rust

```rust
use std::collections::BTreeMap;

impl Solution {
pub fn count_partitions(nums: Vec<i32>, k: i32) -> i32 {
const mod_val: i32 = 1_000_000_007;
let n = nums.len();
let mut f = vec![0; n + 1];
let mut g = vec![0; n + 1];
f[0] = 1;
g[0] = 1;
let mut sl = BTreeMap::new();
let mut l = 1;
for r in 1..=n {
let x = nums[r - 1];
*sl.entry(x).or_insert(0) += 1;
while sl.keys().last().unwrap() - sl.keys().next().unwrap() > k {
let val = nums[l - 1];
if let Some(cnt) = sl.get_mut(&val) {
*cnt -= 1;
if *cnt == 0 {
sl.remove(&val);
}
}
l += 1;
}
f[r] = (g[r - 1] - if l >= 2 { g[l - 2] } else { 0 } + mod_val) % mod_val;
g[r] = (g[r - 1] + f[r]) % mod_val;
}
f[n]
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,42 @@ class TreapMultiSet<T = number> implements ITreapMultiSet<T> {
}
```

#### Rust

```rust
use std::collections::BTreeMap;

impl Solution {
pub fn count_partitions(nums: Vec<i32>, k: i32) -> i32 {
const mod_val: i32 = 1_000_000_007;
let n = nums.len();
let mut f = vec![0; n + 1];
let mut g = vec![0; n + 1];
f[0] = 1;
g[0] = 1;
let mut sl = BTreeMap::new();
let mut l = 1;
for r in 1..=n {
let x = nums[r - 1];
*sl.entry(x).or_insert(0) += 1;
while sl.keys().last().unwrap() - sl.keys().next().unwrap() > k {
let val = nums[l - 1];
if let Some(cnt) = sl.get_mut(&val) {
*cnt -= 1;
if *cnt == 0 {
sl.remove(&val);
}
}
l += 1;
}
f[r] = (g[r - 1] - if l >= 2 { g[l - 2] } else { 0 } + mod_val) % mod_val;
g[r] = (g[r - 1] + f[r]) % mod_val;
}
f[n]
}
}
```

<!-- tabs:end -->

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

impl Solution {
pub fn count_partitions(nums: Vec<i32>, k: i32) -> i32 {
const mod_val: i32 = 1_000_000_007;
let n = nums.len();
let mut f = vec![0; n + 1];
let mut g = vec![0; n + 1];
f[0] = 1;
g[0] = 1;
let mut sl = BTreeMap::new();
let mut l = 1;
for r in 1..=n {
let x = nums[r - 1];
*sl.entry(x).or_insert(0) += 1;
while sl.keys().last().unwrap() - sl.keys().next().unwrap() > k {
let val = nums[l - 1];
if let Some(cnt) = sl.get_mut(&val) {
*cnt -= 1;
if *cnt == 0 {
sl.remove(&val);
}
}
l += 1;
}
f[r] = (g[r - 1] - if l >= 2 { g[l - 2] } else { 0 } + mod_val) % mod_val;
g[r] = (g[r - 1] + f[r]) % mod_val;
}
f[n]
}
}