diff --git a/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README.md b/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README.md index c5e92483d5789..7ac53a72c3a06 100644 --- a/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README.md +++ b/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README.md @@ -254,6 +254,32 @@ function maximumProfit(prices: number[], k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn maximum_profit(prices: Vec, 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] + } +} +``` + diff --git a/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README_EN.md b/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README_EN.md index e0244d7823004..8a07794eb32bd 100644 --- a/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README_EN.md +++ b/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README_EN.md @@ -252,6 +252,32 @@ function maximumProfit(prices: number[], k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn maximum_profit(prices: Vec, 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] + } +} +``` + diff --git a/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/Solution.rs b/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/Solution.rs new file mode 100644 index 0000000000000..0d21af7dae00f --- /dev/null +++ b/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/Solution.rs @@ -0,0 +1,21 @@ +impl Solution { + pub fn maximum_profit(prices: Vec, 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] + } +} diff --git a/solution/3500-3599/3576.Transform Array to All Equal Elements/README.md b/solution/3500-3599/3576.Transform Array to All Equal Elements/README.md index 0655423c41de1..46d631812b168 100644 --- a/solution/3500-3599/3576.Transform Array to All Equal Elements/README.md +++ b/solution/3500-3599/3576.Transform Array to All Equal Elements/README.md @@ -203,6 +203,31 @@ function canMakeEqual(nums: number[], k: number): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn can_make_equal(nums: Vec, k: i32) -> bool { + fn check(target: i32, k: i32, nums: &Vec) -> 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) + } +} +``` + diff --git a/solution/3500-3599/3576.Transform Array to All Equal Elements/README_EN.md b/solution/3500-3599/3576.Transform Array to All Equal Elements/README_EN.md index 97ff3a62badab..eeec55b49ffb0 100644 --- a/solution/3500-3599/3576.Transform Array to All Equal Elements/README_EN.md +++ b/solution/3500-3599/3576.Transform Array to All Equal Elements/README_EN.md @@ -201,6 +201,31 @@ function canMakeEqual(nums: number[], k: number): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn can_make_equal(nums: Vec, k: i32) -> bool { + fn check(target: i32, k: i32, nums: &Vec) -> 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) + } +} +``` + diff --git a/solution/3500-3599/3576.Transform Array to All Equal Elements/Solution.rs b/solution/3500-3599/3576.Transform Array to All Equal Elements/Solution.rs new file mode 100644 index 0000000000000..402458e521337 --- /dev/null +++ b/solution/3500-3599/3576.Transform Array to All Equal Elements/Solution.rs @@ -0,0 +1,20 @@ +impl Solution { + pub fn can_make_equal(nums: Vec, k: i32) -> bool { + fn check(target: i32, k: i32, nums: &Vec) -> 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) + } +} diff --git a/solution/3500-3599/3577.Count the Number of Computer Unlocking Permutations/README.md b/solution/3500-3599/3577.Count the Number of Computer Unlocking Permutations/README.md index 55bc6e4c95e0d..9578188c81829 100644 --- a/solution/3500-3599/3577.Count the Number of Computer Unlocking Permutations/README.md +++ b/solution/3500-3599/3577.Count the Number of Computer Unlocking Permutations/README.md @@ -187,6 +187,24 @@ function countPermutations(complexity: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn count_permutations(complexity: Vec) -> 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 + } +} +``` + diff --git a/solution/3500-3599/3577.Count the Number of Computer Unlocking Permutations/README_EN.md b/solution/3500-3599/3577.Count the Number of Computer Unlocking Permutations/README_EN.md index bcdb67ac00570..5495e1f747112 100644 --- a/solution/3500-3599/3577.Count the Number of Computer Unlocking Permutations/README_EN.md +++ b/solution/3500-3599/3577.Count the Number of Computer Unlocking Permutations/README_EN.md @@ -183,6 +183,24 @@ function countPermutations(complexity: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn count_permutations(complexity: Vec) -> 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 + } +} +``` + diff --git a/solution/3500-3599/3577.Count the Number of Computer Unlocking Permutations/Solution.rs b/solution/3500-3599/3577.Count the Number of Computer Unlocking Permutations/Solution.rs new file mode 100644 index 0000000000000..734bc2dde401f --- /dev/null +++ b/solution/3500-3599/3577.Count the Number of Computer Unlocking Permutations/Solution.rs @@ -0,0 +1,13 @@ +impl Solution { + pub fn count_permutations(complexity: Vec) -> 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 + } +} diff --git a/solution/3500-3599/3578.Count Partitions With Max-Min Difference at Most K/README.md b/solution/3500-3599/3578.Count Partitions With Max-Min Difference at Most K/README.md index ebb8b36472383..e37cb56a1b393 100644 --- a/solution/3500-3599/3578.Count Partitions With Max-Min Difference at Most K/README.md +++ b/solution/3500-3599/3578.Count Partitions With Max-Min Difference at Most K/README.md @@ -854,6 +854,42 @@ class TreapMultiSet implements ITreapMultiSet { } ``` +#### Rust + +```rust + use std::collections::BTreeMap; + +impl Solution { + pub fn count_partitions(nums: Vec, 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] + } +} +``` + diff --git a/solution/3500-3599/3578.Count Partitions With Max-Min Difference at Most K/README_EN.md b/solution/3500-3599/3578.Count Partitions With Max-Min Difference at Most K/README_EN.md index 383fa2a1ed2ff..236f9b676b0d9 100644 --- a/solution/3500-3599/3578.Count Partitions With Max-Min Difference at Most K/README_EN.md +++ b/solution/3500-3599/3578.Count Partitions With Max-Min Difference at Most K/README_EN.md @@ -851,6 +851,42 @@ class TreapMultiSet implements ITreapMultiSet { } ``` +#### Rust + +```rust +use std::collections::BTreeMap; + +impl Solution { + pub fn count_partitions(nums: Vec, 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] + } +} +``` + diff --git a/solution/3500-3599/3578.Count Partitions With Max-Min Difference at Most K/Solution.rs b/solution/3500-3599/3578.Count Partitions With Max-Min Difference at Most K/Solution.rs new file mode 100644 index 0000000000000..1804e4c0faf44 --- /dev/null +++ b/solution/3500-3599/3578.Count Partitions With Max-Min Difference at Most K/Solution.rs @@ -0,0 +1,31 @@ +use std::collections::BTreeMap; + +impl Solution { + pub fn count_partitions(nums: Vec, 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] + } +}