feat: add initial implementation of unique paths with obstacles solution
This commit is contained in:
parent
d757307936
commit
137adf2926
7
63_unique_path_2/Cargo.lock
generated
Normal file
7
63_unique_path_2/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unique_path_2"
|
||||||
|
version = "0.1.0"
|
6
63_unique_path_2/Cargo.toml
Normal file
6
63_unique_path_2/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "unique_path_2"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
36
63_unique_path_2/src/main.rs
Normal file
36
63_unique_path_2/src/main.rs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
fn main() {
|
||||||
|
println!(
|
||||||
|
"{}",
|
||||||
|
Solution::unique_paths_with_obstacles(vec![vec![0], vec![1]])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Solution;
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn unique_paths_with_obstacles(obstacle_grid: Vec<Vec<i32>>) -> i32 {
|
||||||
|
if obstacle_grid[0][0] == 1 {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let m = obstacle_grid.len();
|
||||||
|
let n = obstacle_grid.first().unwrap().len();
|
||||||
|
|
||||||
|
let mut grid = vec![0; n];
|
||||||
|
grid[0] = 1;
|
||||||
|
|
||||||
|
for i in 0..m {
|
||||||
|
for j in 0..n {
|
||||||
|
if obstacle_grid[i][j] == 1 {
|
||||||
|
grid[j] = 0;
|
||||||
|
} else {
|
||||||
|
if j > 0 {
|
||||||
|
grid[j] += grid[j - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*grid.last().unwrap()
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user