Skip to content

Commit 102ce22

Browse files
authored
feat: add rust solutions to lc problems: No.3573,3577,3578 (#4484)
1 parent 128977c commit 102ce22

File tree

12 files changed

+295
-0
lines changed

12 files changed

+295
-0
lines changed

solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,32 @@ function maximumProfit(prices: number[], k: number): number {
254254
}
255255
```
256256

257+
#### Rust
258+
259+
```rust
260+
impl Solution {
261+
pub fn maximum_profit(prices: Vec<i32>, k: i32) -> i64 {
262+
let n = prices.len();
263+
let k = k as usize;
264+
let mut f = vec![vec![vec![0i64; 3]; k + 1]; n];
265+
for j in 1..=k {
266+
f[0][j][1] = -(prices[0] as i64);
267+
f[0][j][2] = prices[0] as i64;
268+
}
269+
for i in 1..n {
270+
for j in 1..=k {
271+
f[i][j][0] = f[i - 1][j][0]
272+
.max(f[i - 1][j][1] + prices[i] as i64)
273+
.max(f[i - 1][j][2] - prices[i] as i64);
274+
f[i][j][1] = f[i - 1][j][1].max(f[i - 1][j - 1][0] - prices[i] as i64);
275+
f[i][j][2] = f[i - 1][j][2].max(f[i - 1][j - 1][0] + prices[i] as i64);
276+
}
277+
}
278+
f[n - 1][k][0]
279+
}
280+
}
281+
```
282+
257283
<!-- tabs:end -->
258284

259285
<!-- solution:end -->

solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README_EN.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,32 @@ function maximumProfit(prices: number[], k: number): number {
252252
}
253253
```
254254

255+
#### Rust
256+
257+
```rust
258+
impl Solution {
259+
pub fn maximum_profit(prices: Vec<i32>, k: i32) -> i64 {
260+
let n = prices.len();
261+
let k = k as usize;
262+
let mut f = vec![vec![vec![0i64; 3]; k + 1]; n];
263+
for j in 1..=k {
264+
f[0][j][1] = -(prices[0] as i64);
265+
f[0][j][2] = prices[0] as i64;
266+
}
267+
for i in 1..n {
268+
for j in 1..=k {
269+
f[i][j][0] = f[i - 1][j][0]
270+
.max(f[i - 1][j][1] + prices[i] as i64)
271+
.max(f[i - 1][j][2] - prices[i] as i64);
272+
f[i][j][1] = f[i - 1][j][1].max(f[i - 1][j - 1][0] - prices[i] as i64);
273+
f[i][j][2] = f[i - 1][j][2].max(f[i - 1][j - 1][0] + prices[i] as i64);
274+
}
275+
}
276+
f[n - 1][k][0]
277+
}
278+
}
279+
```
280+
255281
<!-- tabs:end -->
256282

257283
<!-- solution:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn maximum_profit(prices: Vec<i32>, k: i32) -> i64 {
3+
let n = prices.len();
4+
let k = k as usize;
5+
let mut f = vec![vec![vec![0i64; 3]; k + 1]; n];
6+
for j in 1..=k {
7+
f[0][j][1] = -(prices[0] as i64);
8+
f[0][j][2] = prices[0] as i64;
9+
}
10+
for i in 1..n {
11+
for j in 1..=k {
12+
f[i][j][0] = f[i - 1][j][0]
13+
.max(f[i - 1][j][1] + prices[i] as i64)
14+
.max(f[i - 1][j][2] - prices[i] as i64);
15+
f[i][j][1] = f[i - 1][j][1].max(f[i - 1][j - 1][0] - prices[i] as i64);
16+
f[i][j][2] = f[i - 1][j][2].max(f[i - 1][j - 1][0] + prices[i] as i64);
17+
}
18+
}
19+
f[n - 1][k][0]
20+
}
21+
}

solution/3500-3599/3576.Transform Array to All Equal Elements/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,31 @@ function canMakeEqual(nums: number[], k: number): boolean {
203203
}
204204
```
205205

206+
#### Rust
207+
208+
```rust
209+
impl Solution {
210+
pub fn can_make_equal(nums: Vec<i32>, k: i32) -> bool {
211+
fn check(target: i32, k: i32, nums: &Vec<i32>) -> bool {
212+
let mut cnt = 0;
213+
let mut sign = 1;
214+
for i in 0..nums.len() - 1 {
215+
let x = nums[i] * sign;
216+
if x == target {
217+
sign = 1;
218+
} else {
219+
sign = -1;
220+
cnt += 1;
221+
}
222+
}
223+
cnt <= k && nums[nums.len() - 1] * sign == target
224+
}
225+
226+
check(nums[0], k, &nums) || check(-nums[0], k, &nums)
227+
}
228+
}
229+
```
230+
206231
<!-- tabs:end -->
207232

208233
<!-- solution:end -->

solution/3500-3599/3576.Transform Array to All Equal Elements/README_EN.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,31 @@ function canMakeEqual(nums: number[], k: number): boolean {
201201
}
202202
```
203203

204+
#### Rust
205+
206+
```rust
207+
impl Solution {
208+
pub fn can_make_equal(nums: Vec<i32>, k: i32) -> bool {
209+
fn check(target: i32, k: i32, nums: &Vec<i32>) -> bool {
210+
let mut cnt = 0;
211+
let mut sign = 1;
212+
for i in 0..nums.len() - 1 {
213+
let x = nums[i] * sign;
214+
if x == target {
215+
sign = 1;
216+
} else {
217+
sign = -1;
218+
cnt += 1;
219+
}
220+
}
221+
cnt <= k && nums[nums.len() - 1] * sign == target
222+
}
223+
224+
check(nums[0], k, &nums) || check(-nums[0], k, &nums)
225+
}
226+
}
227+
```
228+
204229
<!-- tabs:end -->
205230

206231
<!-- solution:end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn can_make_equal(nums: Vec<i32>, k: i32) -> bool {
3+
fn check(target: i32, k: i32, nums: &Vec<i32>) -> bool {
4+
let mut cnt = 0;
5+
let mut sign = 1;
6+
for i in 0..nums.len() - 1 {
7+
let x = nums[i] * sign;
8+
if x == target {
9+
sign = 1;
10+
} else {
11+
sign = -1;
12+
cnt += 1;
13+
}
14+
}
15+
cnt <= k && nums[nums.len() - 1] * sign == target
16+
}
17+
18+
check(nums[0], k, &nums) || check(-nums[0], k, &nums)
19+
}
20+
}

solution/3500-3599/3577.Count the Number of Computer Unlocking Permutations/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,24 @@ function countPermutations(complexity: number[]): number {
187187
}
188188
```
189189

190+
#### Rust
191+
192+
```rust
193+
impl Solution {
194+
pub fn count_permutations(complexity: Vec<i32>) -> i32 {
195+
const MOD: i64 = 1_000_000_007;
196+
let mut ans = 1i64;
197+
for i in 1..complexity.len() {
198+
if complexity[i] <= complexity[0] {
199+
return 0;
200+
}
201+
ans = ans * i as i64 % MOD;
202+
}
203+
ans as i32
204+
}
205+
}
206+
```
207+
190208
<!-- tabs:end -->
191209

192210
<!-- solution:end -->

solution/3500-3599/3577.Count the Number of Computer Unlocking Permutations/README_EN.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,24 @@ function countPermutations(complexity: number[]): number {
183183
}
184184
```
185185

186+
#### Rust
187+
188+
```rust
189+
impl Solution {
190+
pub fn count_permutations(complexity: Vec<i32>) -> i32 {
191+
const MOD: i64 = 1_000_000_007;
192+
let mut ans = 1i64;
193+
for i in 1..complexity.len() {
194+
if complexity[i] <= complexity[0] {
195+
return 0;
196+
}
197+
ans = ans * i as i64 % MOD;
198+
}
199+
ans as i32
200+
}
201+
}
202+
```
203+
186204
<!-- tabs:end -->
187205

188206
<!-- 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_permutations(complexity: Vec<i32>) -> i32 {
3+
const MOD: i64 = 1_000_000_007;
4+
let mut ans = 1i64;
5+
for i in 1..complexity.len() {
6+
if complexity[i] <= complexity[0] {
7+
return 0;
8+
}
9+
ans = ans * i as i64 % MOD;
10+
}
11+
ans as i32
12+
}
13+
}

solution/3500-3599/3578.Count Partitions With Max-Min Difference at Most K/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,42 @@ class TreapMultiSet<T = number> implements ITreapMultiSet<T> {
854854
}
855855
```
856856

857+
#### Rust
858+
859+
```rust
860+
use std::collections::BTreeMap;
861+
862+
impl Solution {
863+
pub fn count_partitions(nums: Vec<i32>, k: i32) -> i32 {
864+
const mod_val: i32 = 1_000_000_007;
865+
let n = nums.len();
866+
let mut f = vec![0; n + 1];
867+
let mut g = vec![0; n + 1];
868+
f[0] = 1;
869+
g[0] = 1;
870+
let mut sl = BTreeMap::new();
871+
let mut l = 1;
872+
for r in 1..=n {
873+
let x = nums[r - 1];
874+
*sl.entry(x).or_insert(0) += 1;
875+
while sl.keys().last().unwrap() - sl.keys().next().unwrap() > k {
876+
let val = nums[l - 1];
877+
if let Some(cnt) = sl.get_mut(&val) {
878+
*cnt -= 1;
879+
if *cnt == 0 {
880+
sl.remove(&val);
881+
}
882+
}
883+
l += 1;
884+
}
885+
f[r] = (g[r - 1] - if l >= 2 { g[l - 2] } else { 0 } + mod_val) % mod_val;
886+
g[r] = (g[r - 1] + f[r]) % mod_val;
887+
}
888+
f[n]
889+
}
890+
}
891+
```
892+
857893
<!-- tabs:end -->
858894

859895
<!-- solution:end -->

solution/3500-3599/3578.Count Partitions With Max-Min Difference at Most K/README_EN.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,42 @@ class TreapMultiSet<T = number> implements ITreapMultiSet<T> {
851851
}
852852
```
853853

854+
#### Rust
855+
856+
```rust
857+
use std::collections::BTreeMap;
858+
859+
impl Solution {
860+
pub fn count_partitions(nums: Vec<i32>, k: i32) -> i32 {
861+
const mod_val: i32 = 1_000_000_007;
862+
let n = nums.len();
863+
let mut f = vec![0; n + 1];
864+
let mut g = vec![0; n + 1];
865+
f[0] = 1;
866+
g[0] = 1;
867+
let mut sl = BTreeMap::new();
868+
let mut l = 1;
869+
for r in 1..=n {
870+
let x = nums[r - 1];
871+
*sl.entry(x).or_insert(0) += 1;
872+
while sl.keys().last().unwrap() - sl.keys().next().unwrap() > k {
873+
let val = nums[l - 1];
874+
if let Some(cnt) = sl.get_mut(&val) {
875+
*cnt -= 1;
876+
if *cnt == 0 {
877+
sl.remove(&val);
878+
}
879+
}
880+
l += 1;
881+
}
882+
f[r] = (g[r - 1] - if l >= 2 { g[l - 2] } else { 0 } + mod_val) % mod_val;
883+
g[r] = (g[r - 1] + f[r]) % mod_val;
884+
}
885+
f[n]
886+
}
887+
}
888+
```
889+
854890
<!-- tabs:end -->
855891

856892
<!-- solution:end -->
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::collections::BTreeMap;
2+
3+
impl Solution {
4+
pub fn count_partitions(nums: Vec<i32>, k: i32) -> i32 {
5+
const mod_val: i32 = 1_000_000_007;
6+
let n = nums.len();
7+
let mut f = vec![0; n + 1];
8+
let mut g = vec![0; n + 1];
9+
f[0] = 1;
10+
g[0] = 1;
11+
let mut sl = BTreeMap::new();
12+
let mut l = 1;
13+
for r in 1..=n {
14+
let x = nums[r - 1];
15+
*sl.entry(x).or_insert(0) += 1;
16+
while sl.keys().last().unwrap() - sl.keys().next().unwrap() > k {
17+
let val = nums[l - 1];
18+
if let Some(cnt) = sl.get_mut(&val) {
19+
*cnt -= 1;
20+
if *cnt == 0 {
21+
sl.remove(&val);
22+
}
23+
}
24+
l += 1;
25+
}
26+
f[r] = (g[r - 1] - if l >= 2 { g[l - 2] } else { 0 } + mod_val) % mod_val;
27+
g[r] = (g[r - 1] + f[r]) % mod_val;
28+
}
29+
f[n]
30+
}
31+
}

0 commit comments

Comments
 (0)