diff --git a/solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/README.md b/solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/README.md index 1e7c2bee47fbc..0f1ef0faa1789 100644 --- a/solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/README.md +++ b/solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/README.md @@ -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 = 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, + f: &mut Vec>, + a: &Vec, + ) -> 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) + } +} +``` + diff --git a/solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/README_EN.md b/solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/README_EN.md index 29efc29d419e6..81345a88255a0 100644 --- a/solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/README_EN.md +++ b/solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/README_EN.md @@ -37,7 +37,7 @@ tags:
 Input: floor = "10110101", numCarpets = 2, carpetLen = 2
 Output: 2
-Explanation: 
+Explanation:
 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.
 
@@ -47,7 +47,7 @@ No other way of covering the tiles with the carpets can leave less than 2 white
 Input: floor = "11111", numCarpets = 2, carpetLen = 3
 Output: 0
-Explanation: 
+Explanation:
 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.
 
@@ -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 = 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, + f: &mut Vec>, + a: &Vec, + ) -> 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) + } +} +``` + diff --git a/solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/Solution.rs b/solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/Solution.rs new file mode 100644 index 0000000000000..7740b970edc51 --- /dev/null +++ b/solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/Solution.rs @@ -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 = 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, + f: &mut Vec>, + a: &Vec, + ) -> 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) + } +} diff --git a/solution/2200-2299/2211.Count Collisions on a Road/README.md b/solution/2200-2299/2211.Count Collisions on a Road/README.md index cd4ad43cc4826..d2e6c29f4db62 100644 --- a/solution/2200-2299/2211.Count Collisions on a Road/README.md +++ b/solution/2200-2299/2211.Count Collisions on a Road/README.md @@ -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 diff --git a/solution/2200-2299/2211.Count Collisions on a Road/README_EN.md b/solution/2200-2299/2211.Count Collisions on a Road/README_EN.md index e3057d15d2769..deb1161ba1c4e 100644 --- a/solution/2200-2299/2211.Count Collisions on a Road/README_EN.md +++ b/solution/2200-2299/2211.Count Collisions on a Road/README_EN.md @@ -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.

Example 2:

@@ -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 diff --git a/solution/2200-2299/2211.Count Collisions on a Road/Solution.rs b/solution/2200-2299/2211.Count Collisions on a Road/Solution.rs new file mode 100644 index 0000000000000..09f41f0da4280 --- /dev/null +++ b/solution/2200-2299/2211.Count Collisions on a Road/Solution.rs @@ -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) + } +}