Skip to content
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 @@ -256,6 +256,62 @@ function minimumWhiteTiles(floor: string, numCarpets: number, carpetLen: number)
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_white_tiles(floor: String, num_carpets: i32, carpet_len: i32) -> i32 {
let n = floor.len();
let a: Vec<u8> = floor.bytes().collect();
let m = num_carpets as usize;
let k = carpet_len as usize;

let mut s = vec![0i32; n + 1];
for i in 0..n {
s[i + 1] = s[i] + if a[i] == b'1' { 1 } else { 0 };
}

let mut f = vec![vec![-1; m + 1]; n];

fn dfs(
i: usize,
j: usize,
n: usize,
k: usize,
s: &Vec<i32>,
f: &mut Vec<Vec<i32>>,
a: &Vec<u8>,
) -> i32 {
if i >= n {
return 0;
}
if j == 0 {
return s[n] - s[i];
}
if f[i][j] != -1 {
return f[i][j];
}

if s[i + 1] == s[i] {
let v = dfs(i + 1, j, n, k, s, f, a);
f[i][j] = v;
return v;
}

let t1 = 1 + dfs(i + 1, j, n, k, s, f, a);
let ni = i + k;
let t2 = dfs(ni, j - 1, n, k, s, f, a);

let t = t1.min(t2);
f[i][j] = t;
t
}

dfs(0, m, n, k, &s, &mut f, &a)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ tags:
<pre>
<strong>Input:</strong> floor = &quot;10110101&quot;, numCarpets = 2, carpetLen = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong>
<strong>Explanation:</strong>
The figure above shows one way of covering the tiles with the carpets such that only 2 white tiles are visible.
No other way of covering the tiles with the carpets can leave less than 2 white tiles visible.
</pre>
Expand All @@ -47,7 +47,7 @@ No other way of covering the tiles with the carpets can leave less than 2 white
<pre>
<strong>Input:</strong> floor = &quot;11111&quot;, numCarpets = 2, carpetLen = 3
<strong>Output:</strong> 0
<strong>Explanation:</strong>
<strong>Explanation:</strong>
The figure above shows one way of covering the tiles with the carpets such that no white tiles are visible.
Note that the carpets are able to overlap one another.
</pre>
Expand Down Expand Up @@ -251,6 +251,62 @@ function minimumWhiteTiles(floor: string, numCarpets: number, carpetLen: number)
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_white_tiles(floor: String, num_carpets: i32, carpet_len: i32) -> i32 {
let n = floor.len();
let a: Vec<u8> = floor.bytes().collect();
let m = num_carpets as usize;
let k = carpet_len as usize;

let mut s = vec![0i32; n + 1];
for i in 0..n {
s[i + 1] = s[i] + if a[i] == b'1' { 1 } else { 0 };
}

let mut f = vec![vec![-1; m + 1]; n];

fn dfs(
i: usize,
j: usize,
n: usize,
k: usize,
s: &Vec<i32>,
f: &mut Vec<Vec<i32>>,
a: &Vec<u8>,
) -> i32 {
if i >= n {
return 0;
}
if j == 0 {
return s[n] - s[i];
}
if f[i][j] != -1 {
return f[i][j];
}

if s[i + 1] == s[i] {
let v = dfs(i + 1, j, n, k, s, f, a);
f[i][j] = v;
return v;
}

let t1 = 1 + dfs(i + 1, j, n, k, s, f, a);
let ni = i + k;
let t2 = dfs(ni, j - 1, n, k, s, f, a);

let t = t1.min(t2);
f[i][j] = t;
t
}

dfs(0, m, n, k, &s, &mut f, &a)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
impl Solution {
pub fn minimum_white_tiles(floor: String, num_carpets: i32, carpet_len: i32) -> i32 {
let n = floor.len();
let a: Vec<u8> = floor.bytes().collect();
let m = num_carpets as usize;
let k = carpet_len as usize;

let mut s = vec![0i32; n + 1];
for i in 0..n {
s[i + 1] = s[i] + if a[i] == b'1' { 1 } else { 0 };
}

let mut f = vec![vec![-1; m + 1]; n];

fn dfs(
i: usize,
j: usize,
n: usize,
k: usize,
s: &Vec<i32>,
f: &mut Vec<Vec<i32>>,
a: &Vec<u8>,
) -> i32 {
if i >= n {
return 0;
}
if j == 0 {
return s[n] - s[i];
}
if f[i][j] != -1 {
return f[i][j];
}

if s[i + 1] == s[i] {
let v = dfs(i + 1, j, n, k, s, f, a);
f[i][j] = v;
return v;
}

let t1 = 1 + dfs(i + 1, j, n, k, s, f, a);
let ni = i + k;
let t2 = dfs(ni, j - 1, n, k, s, f, a);

let t = t1.min(t2);
f[i][j] = t;
t
}

dfs(0, m, n, k, &s, &mut f, &a)
}
}
11 changes: 11 additions & 0 deletions solution/2200-2299/2211.Count Collisions on a Road/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ function countCollisions(directions: string): number {
}
```

#### Rust

```rust
impl Solution {
pub fn count_collisions(directions: String) -> i32 {
let s = directions.trim_start_matches('L').trim_end_matches('R');
(s.len() as i32) - (s.matches('S').count() as i32)
}
}
```

#### JavaScript

```js
Expand Down
13 changes: 12 additions & 1 deletion solution/2200-2299/2211.Count Collisions on a Road/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The collisions that will happen on the road are:
- Cars 2 and 3 will collide with each other. Since car 3 is stationary, the number of collisions becomes 2 + 1 = 3.
- Cars 3 and 4 will collide with each other. Since car 3 is stationary, the number of collisions becomes 3 + 1 = 4.
- 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.
Thus, the total number of collisions that will happen on the road is 5.
Thus, the total number of collisions that will happen on the road is 5.
</pre>

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

#### Rust

```rust
impl Solution {
pub fn count_collisions(directions: String) -> i32 {
let s = directions.trim_start_matches('L').trim_end_matches('R');
(s.len() as i32) - (s.matches('S').count() as i32)
}
}
```

#### JavaScript

```js
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
impl Solution {
pub fn count_collisions(directions: String) -> i32 {
let s = directions.trim_start_matches('L').trim_end_matches('R');
(s.len() as i32) - (s.matches('S').count() as i32)
}
}