Skip to content

Commit d6db1b7

Browse files
authored
feat: add rust solutions to lc problems: No.2209,2211 (#4875)
1 parent dd95606 commit d6db1b7

File tree

6 files changed

+194
-3
lines changed

6 files changed

+194
-3
lines changed

solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,62 @@ function minimumWhiteTiles(floor: string, numCarpets: number, carpetLen: number)
256256
}
257257
```
258258

259+
#### Rust
260+
261+
```rust
262+
impl Solution {
263+
pub fn minimum_white_tiles(floor: String, num_carpets: i32, carpet_len: i32) -> i32 {
264+
let n = floor.len();
265+
let a: Vec<u8> = floor.bytes().collect();
266+
let m = num_carpets as usize;
267+
let k = carpet_len as usize;
268+
269+
let mut s = vec![0i32; n + 1];
270+
for i in 0..n {
271+
s[i + 1] = s[i] + if a[i] == b'1' { 1 } else { 0 };
272+
}
273+
274+
let mut f = vec![vec![-1; m + 1]; n];
275+
276+
fn dfs(
277+
i: usize,
278+
j: usize,
279+
n: usize,
280+
k: usize,
281+
s: &Vec<i32>,
282+
f: &mut Vec<Vec<i32>>,
283+
a: &Vec<u8>,
284+
) -> i32 {
285+
if i >= n {
286+
return 0;
287+
}
288+
if j == 0 {
289+
return s[n] - s[i];
290+
}
291+
if f[i][j] != -1 {
292+
return f[i][j];
293+
}
294+
295+
if s[i + 1] == s[i] {
296+
let v = dfs(i + 1, j, n, k, s, f, a);
297+
f[i][j] = v;
298+
return v;
299+
}
300+
301+
let t1 = 1 + dfs(i + 1, j, n, k, s, f, a);
302+
let ni = i + k;
303+
let t2 = dfs(ni, j - 1, n, k, s, f, a);
304+
305+
let t = t1.min(t2);
306+
f[i][j] = t;
307+
t
308+
}
309+
310+
dfs(0, m, n, k, &s, &mut f, &a)
311+
}
312+
}
313+
```
314+
259315
<!-- tabs:end -->
260316

261317
<!-- solution:end -->

solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/README_EN.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ tags:
3737
<pre>
3838
<strong>Input:</strong> floor = &quot;10110101&quot;, numCarpets = 2, carpetLen = 2
3939
<strong>Output:</strong> 2
40-
<strong>Explanation:</strong>
40+
<strong>Explanation:</strong>
4141
The figure above shows one way of covering the tiles with the carpets such that only 2 white tiles are visible.
4242
No other way of covering the tiles with the carpets can leave less than 2 white tiles visible.
4343
</pre>
@@ -47,7 +47,7 @@ No other way of covering the tiles with the carpets can leave less than 2 white
4747
<pre>
4848
<strong>Input:</strong> floor = &quot;11111&quot;, numCarpets = 2, carpetLen = 3
4949
<strong>Output:</strong> 0
50-
<strong>Explanation:</strong>
50+
<strong>Explanation:</strong>
5151
The figure above shows one way of covering the tiles with the carpets such that no white tiles are visible.
5252
Note that the carpets are able to overlap one another.
5353
</pre>
@@ -251,6 +251,62 @@ function minimumWhiteTiles(floor: string, numCarpets: number, carpetLen: number)
251251
}
252252
```
253253

254+
#### Rust
255+
256+
```rust
257+
impl Solution {
258+
pub fn minimum_white_tiles(floor: String, num_carpets: i32, carpet_len: i32) -> i32 {
259+
let n = floor.len();
260+
let a: Vec<u8> = floor.bytes().collect();
261+
let m = num_carpets as usize;
262+
let k = carpet_len as usize;
263+
264+
let mut s = vec![0i32; n + 1];
265+
for i in 0..n {
266+
s[i + 1] = s[i] + if a[i] == b'1' { 1 } else { 0 };
267+
}
268+
269+
let mut f = vec![vec![-1; m + 1]; n];
270+
271+
fn dfs(
272+
i: usize,
273+
j: usize,
274+
n: usize,
275+
k: usize,
276+
s: &Vec<i32>,
277+
f: &mut Vec<Vec<i32>>,
278+
a: &Vec<u8>,
279+
) -> i32 {
280+
if i >= n {
281+
return 0;
282+
}
283+
if j == 0 {
284+
return s[n] - s[i];
285+
}
286+
if f[i][j] != -1 {
287+
return f[i][j];
288+
}
289+
290+
if s[i + 1] == s[i] {
291+
let v = dfs(i + 1, j, n, k, s, f, a);
292+
f[i][j] = v;
293+
return v;
294+
}
295+
296+
let t1 = 1 + dfs(i + 1, j, n, k, s, f, a);
297+
let ni = i + k;
298+
let t2 = dfs(ni, j - 1, n, k, s, f, a);
299+
300+
let t = t1.min(t2);
301+
f[i][j] = t;
302+
t
303+
}
304+
305+
dfs(0, m, n, k, &s, &mut f, &a)
306+
}
307+
}
308+
```
309+
254310
<!-- tabs:end -->
255311

256312
<!-- solution:end -->
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
impl Solution {
2+
pub fn minimum_white_tiles(floor: String, num_carpets: i32, carpet_len: i32) -> i32 {
3+
let n = floor.len();
4+
let a: Vec<u8> = floor.bytes().collect();
5+
let m = num_carpets as usize;
6+
let k = carpet_len as usize;
7+
8+
let mut s = vec![0i32; n + 1];
9+
for i in 0..n {
10+
s[i + 1] = s[i] + if a[i] == b'1' { 1 } else { 0 };
11+
}
12+
13+
let mut f = vec![vec![-1; m + 1]; n];
14+
15+
fn dfs(
16+
i: usize,
17+
j: usize,
18+
n: usize,
19+
k: usize,
20+
s: &Vec<i32>,
21+
f: &mut Vec<Vec<i32>>,
22+
a: &Vec<u8>,
23+
) -> i32 {
24+
if i >= n {
25+
return 0;
26+
}
27+
if j == 0 {
28+
return s[n] - s[i];
29+
}
30+
if f[i][j] != -1 {
31+
return f[i][j];
32+
}
33+
34+
if s[i + 1] == s[i] {
35+
let v = dfs(i + 1, j, n, k, s, f, a);
36+
f[i][j] = v;
37+
return v;
38+
}
39+
40+
let t1 = 1 + dfs(i + 1, j, n, k, s, f, a);
41+
let ni = i + k;
42+
let t2 = dfs(ni, j - 1, n, k, s, f, a);
43+
44+
let t = t1.min(t2);
45+
f[i][j] = t;
46+
t
47+
}
48+
49+
dfs(0, m, n, k, &s, &mut f, &a)
50+
}
51+
}

solution/2200-2299/2211.Count Collisions on a Road/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ function countCollisions(directions: string): number {
166166
}
167167
```
168168

169+
#### Rust
170+
171+
```rust
172+
impl Solution {
173+
pub fn count_collisions(directions: String) -> i32 {
174+
let s = directions.trim_start_matches('L').trim_end_matches('R');
175+
(s.len() as i32) - (s.matches('S').count() as i32)
176+
}
177+
}
178+
```
179+
169180
#### JavaScript
170181

171182
```js

solution/2200-2299/2211.Count Collisions on a Road/README_EN.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ The collisions that will happen on the road are:
4747
- Cars 2 and 3 will collide with each other. Since car 3 is stationary, the number of collisions becomes 2 + 1 = 3.
4848
- Cars 3 and 4 will collide with each other. Since car 3 is stationary, the number of collisions becomes 3 + 1 = 4.
4949
- Cars 4 and 5 will collide with each other. After car 4 collides with car 3, it will stay at the point of collision and get hit by car 5. The number of collisions becomes 4 + 1 = 5.
50-
Thus, the total number of collisions that will happen on the road is 5.
50+
Thus, the total number of collisions that will happen on the road is 5.
5151
</pre>
5252

5353
<p><strong class="example">Example 2:</strong></p>
@@ -164,6 +164,17 @@ function countCollisions(directions: string): number {
164164
}
165165
```
166166

167+
#### Rust
168+
169+
```rust
170+
impl Solution {
171+
pub fn count_collisions(directions: String) -> i32 {
172+
let s = directions.trim_start_matches('L').trim_end_matches('R');
173+
(s.len() as i32) - (s.matches('S').count() as i32)
174+
}
175+
}
176+
```
177+
167178
#### JavaScript
168179

169180
```js
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
impl Solution {
2+
pub fn count_collisions(directions: String) -> i32 {
3+
let s = directions.trim_start_matches('L').trim_end_matches('R');
4+
(s.len() as i32) - (s.matches('S').count() as i32)
5+
}
6+
}

0 commit comments

Comments
 (0)