Skip to content

Commit ec3dbde

Browse files
authored
feat: add Rust solution for lc No.3129 (#5068)
1 parent cf78978 commit ec3dbde

4 files changed

Lines changed: 297 additions & 17 deletions

File tree

solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README.md

Lines changed: 101 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,19 @@ tags:
8989

9090
### 方法一:记忆化搜索
9191

92-
我们设计一个函数 $dfs(i, j, k)$ 表示还剩下 $i$ 个 $0$ 和 $j$ 个 $1$ 且接下来待填的数字是 $k$ 的情况下,满足题目条件的稳定二进制数组的个数。那么答案就是 $dfs(zero, one, 0) + dfs(zero, one, 1)$。
92+
我们设计一个函数 $\textit{dfs}(i, j, k)$ 表示还剩下 $i$ 个 $0$ 和 $j$ 个 $1$ 且接下来待填的数字是 $k$ 的情况下,满足题目条件的稳定二进制数组的个数。那么答案就是 $\textit{dfs}(\textit{zero}, \textit{one}, 0) + \textit{dfs}(\textit{zero}, \textit{one}, 1)$。
9393

94-
函数 $dfs(i, j, k)$ 的计算过程如下:
94+
函数 $\textit{dfs}(i, j, k)$ 的计算过程如下:
9595

9696
- 如果 $i \lt 0$ 或 $j \lt 0$,返回 $0$。
9797
- 如果 $i = 0$,那么当 $k = 1$ 且 $j \leq \textit{limit}$ 时返回 $1$,否则返回 $0$。
9898
- 如果 $j = 0$,那么当 $k = 0$ 且 $i \leq \textit{limit}$ 时返回 $1$,否则返回 $0$。
99-
- 如果 $k = 0$,我们考虑前一个数字是 $0$ 的情况 $dfs(i - 1, j, 0)$ 和前一个数字是 $1$ 的情况 $dfs(i - 1, j, 1)$,如果前一个数是 $0$,那么有可能使得子数组中有超过 $\textit{limit}$ 个 $0$,即不允许出现倒数第 $\textit{limit} + 1$ 个数是 $1$ 的情况,所以我们要减去这种情况,即 $dfs(i - \textit{limit} - 1, j, 1)$。
100-
- 如果 $k = 1$,我们考虑前一个数字是 $0$ 的情况 $dfs(i, j - 1, 0)$ 和前一个数字是 $1$ 的情况 $dfs(i, j - 1, 1)$,如果前一个数是 $1$,那么有可能使得子数组中有超过 $\textit{limit}$ 个 $1$,即不允许出现倒数第 $\textit{limit} + 1$ 个数是 $0$ 的情况,所以我们要减去这种情况,即 $dfs(i, j - \textit{limit} - 1, 0)$。
99+
- 如果 $k = 0$,我们考虑前一个数字是 $0$ 的情况 $\textit{dfs}(i - 1, j, 0)$ 和前一个数字是 $1$ 的情况 $\textit{dfs}(i - 1, j, 1)$,如果前一个数是 $0$,那么有可能使得子数组中有超过 $\textit{limit}$ 个 $0$,即不允许出现倒数第 $\textit{limit} + 1$ 个数是 $1$ 的情况,所以我们要减去这种情况,即 $\textit{dfs}(i - \textit{limit} - 1, j, 1)$。
100+
- 如果 $k = 1$,我们考虑前一个数字是 $0$ 的情况 $\textit{dfs}(i, j - 1, 0)$ 和前一个数字是 $1$ 的情况 $\textit{dfs}(i, j - 1, 1)$,如果前一个数是 $1$,那么有可能使得子数组中有超过 $\textit{limit}$ 个 $1$,即不允许出现倒数第 $\textit{limit} + 1$ 个数是 $0$ 的情况,所以我们要减去这种情况,即 $\textit{dfs}(i, j - \textit{limit} - 1, 0)$。
101101

102102
为了避免重复计算,我们使用记忆化搜索的方法。
103103

104-
时间复杂度 $O(zero \times one)$,空间复杂度 $O(zero \times one)$。
104+
时间复杂度 $O(\textit{zero} \times \textit{one})$,空间复杂度 $O(\textit{zero} \times \textit{one})$。
105105

106106
<!-- tabs:start -->
107107

@@ -287,6 +287,65 @@ function numberOfStableArrays(zero: number, one: number, limit: number): number
287287
}
288288
```
289289

290+
#### Rust
291+
292+
```rust
293+
impl Solution {
294+
pub fn number_of_stable_arrays(zero: i32, one: i32, limit: i32) -> i32 {
295+
const MOD: i64 = 1_000_000_007;
296+
297+
fn dfs(
298+
i: i32,
299+
j: i32,
300+
k: usize,
301+
limit: i32,
302+
f: &mut Vec<Vec<[i64; 2]>>,
303+
) -> i64 {
304+
if i < 0 || j < 0 {
305+
return 0;
306+
}
307+
308+
if i == 0 {
309+
return if k == 1 && j <= limit { 1 } else { 0 };
310+
}
311+
312+
if j == 0 {
313+
return if k == 0 && i <= limit { 1 } else { 0 };
314+
}
315+
316+
let (iu, ju) = (i as usize, j as usize);
317+
318+
if f[iu][ju][k] != -1 {
319+
return f[iu][ju][k];
320+
}
321+
322+
let res = if k == 0 {
323+
(
324+
dfs(i - 1, j, 0, limit, f)
325+
+ dfs(i - 1, j, 1, limit, f)
326+
- dfs(i - limit - 1, j, 1, limit, f)
327+
+ MOD
328+
) % MOD
329+
} else {
330+
(
331+
dfs(i, j - 1, 0, limit, f)
332+
+ dfs(i, j - 1, 1, limit, f)
333+
- dfs(i, j - limit - 1, 0, limit, f)
334+
+ MOD
335+
) % MOD
336+
};
337+
338+
f[iu][ju][k] = res;
339+
res
340+
}
341+
342+
let mut f = vec![vec![[-1_i64; 2]; (one + 1) as usize]; (zero + 1) as usize];
343+
344+
((dfs(zero, one, 0, limit, &mut f) + dfs(zero, one, 1, limit, &mut f)) % MOD) as i32
345+
}
346+
}
347+
```
348+
290349
<!-- tabs:end -->
291350

292351
<!-- solution:end -->
@@ -306,7 +365,7 @@ function numberOfStableArrays(zero: number, one: number, limit: number): number
306365
- $f[i][j][0] = f[i - 1][j][0] + f[i - 1][j][1] - f[i - \textit{limit} - 1][j][1]$。
307366
- $f[i][j][1] = f[i][j - 1][0] + f[i][j - 1][1] - f[i][j - \textit{limit} - 1][0]$。
308367

309-
时间复杂度 $O(zero \times one)$,空间复杂度 $O(zero \times one)$。
368+
时间复杂度 $O(\textit{zero} \times \textit{one})$,空间复杂度 $O(\textit{zero} \times \textit{one})$。
310369

311370
<!-- tabs:start -->
312371

@@ -445,6 +504,42 @@ function numberOfStableArrays(zero: number, one: number, limit: number): number
445504
}
446505
```
447506

507+
#### Rust
508+
509+
```rust
510+
impl Solution {
511+
pub fn number_of_stable_arrays(zero: i32, one: i32, limit: i32) -> i32 {
512+
let mod_: i64 = 1_000_000_007;
513+
514+
let zero = zero as usize;
515+
let one = one as usize;
516+
let limit = limit as usize;
517+
518+
let mut f = vec![vec![[0_i64; 2]; one + 1]; zero + 1];
519+
520+
for i in 1..=zero.min(limit) {
521+
f[i][0][0] = 1;
522+
}
523+
524+
for j in 1..=one.min(limit) {
525+
f[0][j][1] = 1;
526+
}
527+
528+
for i in 1..=zero {
529+
for j in 1..=one {
530+
let x = if i > limit { f[i - limit - 1][j][1] } else { 0 };
531+
let y = if j > limit { f[i][j - limit - 1][0] } else { 0 };
532+
533+
f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod_) % mod_;
534+
f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod_) % mod_;
535+
}
536+
}
537+
538+
((f[zero][one][0] + f[zero][one][1]) % mod_) as i32
539+
}
540+
}
541+
```
542+
448543
<!-- tabs:end -->
449544

450545
<!-- solution:end -->

solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README_EN.md

Lines changed: 111 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,21 @@ tags:
8585

8686
<!-- solution:start -->
8787

88-
### Solution 1: Memoization Search
88+
### Solution 1: Memoized Search
8989

90-
We design a function $dfs(i, j, k)$ to represent the number of stable binary arrays that satisfy the problem conditions when there are $i$ $0$s and $j$ $1$s left, and the next number to be filled is $k$. The answer is $dfs(zero, one, 0) + dfs(zero, one, 1)$.
90+
We define a function $\textit{dfs}(i, j, k)$ to represent the number of stable binary arrays that satisfy the problem conditions when there are $i$ zeros and $j$ ones remaining to place, and the next digit to fill is $k$. Then the answer is $\textit{dfs}(\textit{zero}, \textit{one}, 0) + \textit{dfs}(\textit{zero}, \textit{one}, 1)$.
9191

92-
The calculation process of the function $dfs(i, j, k)$ is as follows:
92+
The computation process of $\textit{dfs}(i, j, k)$ is as follows:
9393

94-
- If $i < 0$ or $j < 0$, return $0$.
95-
- If $i = 0$, return $1$ when $k = 1$ and $j \leq \textit{limit}$, otherwise return $0$.
96-
- If $j = 0$, return $1$ when $k = 0$ and $i \leq \textit{limit}$, otherwise return $0$.
97-
- If $k = 0$, we consider the case where the previous number is $0$, $dfs(i - 1, j, 0)$, and the case where the previous number is $1$, $dfs(i - 1, j, 1)$. If the previous number is $0$, it may cause more than $\textit{limit}$ $0$s in the subarray, i.e., the situation where the $\textit{limit} + 1$
94+
- If $i \lt 0$ or $j \lt 0$, return $0$.
95+
- If $i = 0$, then return $1$ when $k = 1$ and $j \leq \textit{limit}$; otherwise return $0$.
96+
- If $j = 0$, then return $1$ when $k = 0$ and $i \leq \textit{limit}$; otherwise return $0$.
97+
- If $k = 0$, we consider the case where the previous digit is $0$, i.e., $\textit{dfs}(i - 1, j, 0)$, and the case where the previous digit is $1$, i.e., $\textit{dfs}(i - 1, j, 1)$. If the previous digit is $0$, there may be more than $\textit{limit}$ zeros in a subarray, which means the case where the $(\textit{limit} + 1)$-th digit from the end is $1$ is invalid, so we subtract this case: $\textit{dfs}(i - \textit{limit} - 1, j, 1)$.
98+
- If $k = 1$, we consider the case where the previous digit is $0$, i.e., $\textit{dfs}(i, j - 1, 0)$, and the case where the previous digit is $1$, i.e., $\textit{dfs}(i, j - 1, 1)$. If the previous digit is $1$, there may be more than $\textit{limit}$ ones in a subarray, which means the case where the $(\textit{limit} + 1)$-th digit from the end is $0$ is invalid, so we subtract this case: $\textit{dfs}(i, j - \textit{limit} - 1, 0)$.
99+
100+
To avoid repeated computation, we use memoized search.
101+
102+
The time complexity is $O(\textit{zero} \times \textit{one})$, and the space complexity is $O(\textit{zero} \times \textit{one})$.
98103

99104
<!-- tabs:start -->
100105

@@ -278,6 +283,65 @@ function numberOfStableArrays(zero: number, one: number, limit: number): number
278283
}
279284
```
280285

286+
#### Rust
287+
288+
```rust
289+
impl Solution {
290+
pub fn number_of_stable_arrays(zero: i32, one: i32, limit: i32) -> i32 {
291+
const MOD: i64 = 1_000_000_007;
292+
293+
fn dfs(
294+
i: i32,
295+
j: i32,
296+
k: usize,
297+
limit: i32,
298+
f: &mut Vec<Vec<[i64; 2]>>,
299+
) -> i64 {
300+
if i < 0 || j < 0 {
301+
return 0;
302+
}
303+
304+
if i == 0 {
305+
return if k == 1 && j <= limit { 1 } else { 0 };
306+
}
307+
308+
if j == 0 {
309+
return if k == 0 && i <= limit { 1 } else { 0 };
310+
}
311+
312+
let (iu, ju) = (i as usize, j as usize);
313+
314+
if f[iu][ju][k] != -1 {
315+
return f[iu][ju][k];
316+
}
317+
318+
let res = if k == 0 {
319+
(
320+
dfs(i - 1, j, 0, limit, f)
321+
+ dfs(i - 1, j, 1, limit, f)
322+
- dfs(i - limit - 1, j, 1, limit, f)
323+
+ MOD
324+
) % MOD
325+
} else {
326+
(
327+
dfs(i, j - 1, 0, limit, f)
328+
+ dfs(i, j - 1, 1, limit, f)
329+
- dfs(i, j - limit - 1, 0, limit, f)
330+
+ MOD
331+
) % MOD
332+
};
333+
334+
f[iu][ju][k] = res;
335+
res
336+
}
337+
338+
let mut f = vec![vec![[-1_i64; 2]; (one + 1) as usize]; (zero + 1) as usize];
339+
340+
((dfs(zero, one, 0, limit, &mut f) + dfs(zero, one, 1, limit, &mut f)) % MOD) as i32
341+
}
342+
}
343+
```
344+
281345
<!-- tabs:end -->
282346

283347
<!-- solution:end -->
@@ -286,18 +350,18 @@ function numberOfStableArrays(zero: number, one: number, limit: number): number
286350

287351
### Solution 2: Dynamic Programming
288352

289-
We can also convert the memoization search of Solution 1 into dynamic programming.
353+
We can also convert the memoized search in Solution 1 into dynamic programming.
290354

291-
We define $f[i][j][k]$ as the number of stable binary arrays using $i$ $0$s and $j$ $1$s, and the last number is $k$. So the answer is $f[zero][one][0] + f[zero][one][1]$.
355+
We define $f[i][j][k]$ as the number of stable binary arrays that use $i$ zeros and $j$ ones, with the last digit being $k$. Then the answer is $f[zero][one][0] + f[zero][one][1]$.
292356

293357
Initially, we have $f[i][0][0] = 1$, where $1 \leq i \leq \min(\textit{limit}, \textit{zero})$; and $f[0][j][1] = 1$, where $1 \leq j \leq \min(\textit{limit}, \textit{one})$.
294358

295-
The state transition equation is as follows:
359+
The state transition equations are as follows:
296360

297361
- $f[i][j][0] = f[i - 1][j][0] + f[i - 1][j][1] - f[i - \textit{limit} - 1][j][1]$.
298362
- $f[i][j][1] = f[i][j - 1][0] + f[i][j - 1][1] - f[i][j - \textit{limit} - 1][0]$.
299363

300-
The time complexity is $O(zero \times one)$, and the space complexity is $O(zero \times one)$.
364+
The time complexity is $O(\textit{zero} \times \textit{one})$, and the space complexity is $O(\textit{zero} \times \textit{one})$.
301365

302366
<!-- tabs:start -->
303367

@@ -436,6 +500,42 @@ function numberOfStableArrays(zero: number, one: number, limit: number): number
436500
}
437501
```
438502

503+
#### Rust
504+
505+
```rust
506+
impl Solution {
507+
pub fn number_of_stable_arrays(zero: i32, one: i32, limit: i32) -> i32 {
508+
let mod_: i64 = 1_000_000_007;
509+
510+
let zero = zero as usize;
511+
let one = one as usize;
512+
let limit = limit as usize;
513+
514+
let mut f = vec![vec![[0_i64; 2]; one + 1]; zero + 1];
515+
516+
for i in 1..=zero.min(limit) {
517+
f[i][0][0] = 1;
518+
}
519+
520+
for j in 1..=one.min(limit) {
521+
f[0][j][1] = 1;
522+
}
523+
524+
for i in 1..=zero {
525+
for j in 1..=one {
526+
let x = if i > limit { f[i - limit - 1][j][1] } else { 0 };
527+
let y = if j > limit { f[i][j - limit - 1][0] } else { 0 };
528+
529+
f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod_) % mod_;
530+
f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod_) % mod_;
531+
}
532+
}
533+
534+
((f[zero][one][0] + f[zero][one][1]) % mod_) as i32
535+
}
536+
}
537+
```
538+
439539
<!-- tabs:end -->
440540

441541
<!-- solution:end -->
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
impl Solution {
2+
pub fn number_of_stable_arrays(zero: i32, one: i32, limit: i32) -> i32 {
3+
const MOD: i64 = 1_000_000_007;
4+
5+
fn dfs(
6+
i: i32,
7+
j: i32,
8+
k: usize,
9+
limit: i32,
10+
f: &mut Vec<Vec<[i64; 2]>>,
11+
) -> i64 {
12+
if i < 0 || j < 0 {
13+
return 0;
14+
}
15+
16+
if i == 0 {
17+
return if k == 1 && j <= limit { 1 } else { 0 };
18+
}
19+
20+
if j == 0 {
21+
return if k == 0 && i <= limit { 1 } else { 0 };
22+
}
23+
24+
let (iu, ju) = (i as usize, j as usize);
25+
26+
if f[iu][ju][k] != -1 {
27+
return f[iu][ju][k];
28+
}
29+
30+
let res = if k == 0 {
31+
(
32+
dfs(i - 1, j, 0, limit, f)
33+
+ dfs(i - 1, j, 1, limit, f)
34+
- dfs(i - limit - 1, j, 1, limit, f)
35+
+ MOD
36+
) % MOD
37+
} else {
38+
(
39+
dfs(i, j - 1, 0, limit, f)
40+
+ dfs(i, j - 1, 1, limit, f)
41+
- dfs(i, j - limit - 1, 0, limit, f)
42+
+ MOD
43+
) % MOD
44+
};
45+
46+
f[iu][ju][k] = res;
47+
res
48+
}
49+
50+
let mut f = vec![vec![[-1_i64; 2]; (one + 1) as usize]; (zero + 1) as usize];
51+
52+
((dfs(zero, one, 0, limit, &mut f) + dfs(zero, one, 1, limit, &mut f)) % MOD) as i32
53+
}
54+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
impl Solution {
2+
pub fn number_of_stable_arrays(zero: i32, one: i32, limit: i32) -> i32 {
3+
let mod_: i64 = 1_000_000_007;
4+
5+
let zero = zero as usize;
6+
let one = one as usize;
7+
let limit = limit as usize;
8+
9+
let mut f = vec![vec![[0_i64; 2]; one + 1]; zero + 1];
10+
11+
for i in 1..=zero.min(limit) {
12+
f[i][0][0] = 1;
13+
}
14+
15+
for j in 1..=one.min(limit) {
16+
f[0][j][1] = 1;
17+
}
18+
19+
for i in 1..=zero {
20+
for j in 1..=one {
21+
let x = if i > limit { f[i - limit - 1][j][1] } else { 0 };
22+
let y = if j > limit { f[i][j - limit - 1][0] } else { 0 };
23+
24+
f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod_) % mod_;
25+
f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod_) % mod_;
26+
}
27+
}
28+
29+
((f[zero][one][0] + f[zero][one][1]) % mod_) as i32
30+
}
31+
}

0 commit comments

Comments
 (0)