feat: add initial implementation of unique paths with obstacles solution

This commit is contained in:
SquidSpirit 2025-08-16 23:52:40 +08:00
parent d757307936
commit 137adf2926
3 changed files with 49 additions and 0 deletions

7
63_unique_path_2/Cargo.lock generated Normal file
View 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"

View File

@ -0,0 +1,6 @@
[package]
name = "unique_path_2"
version = "0.1.0"
edition = "2024"
[dependencies]

View 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()
}
}