20240810 Finished
This commit is contained in:
parent
7fab520904
commit
a602f53852
|
@ -199,4 +199,5 @@ mod p198_house_robber;
|
|||
mod p139_word_break;
|
||||
mod p300_longest_increasing_subsequence;
|
||||
mod p120_triangle;
|
||||
mod p64_minimum_path_sum;
|
||||
mod p64_minimum_path_sum;
|
||||
mod p63_unique_paths_ii;
|
55
src/problem/p63_unique_paths_ii.rs
Normal file
55
src/problem/p63_unique_paths_ii.rs
Normal file
|
@ -0,0 +1,55 @@
|
|||
/**
|
||||
* [63] Unique Paths II
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn unique_paths_with_obstacles(obstacle_grid: Vec<Vec<i32>>) -> i32 {
|
||||
let (m, n) = (obstacle_grid.len(), obstacle_grid[0].len());
|
||||
let mut dp = vec![vec![0; n]; m];
|
||||
dp[0][0] = if obstacle_grid[0][0] == 1 {
|
||||
0
|
||||
} else {
|
||||
1
|
||||
};
|
||||
|
||||
for i in 0..m {
|
||||
for j in 0..n {
|
||||
if obstacle_grid[i][j] == 1 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut value = 0;
|
||||
if i != 0 {
|
||||
value += dp[i - 1][j];
|
||||
}
|
||||
|
||||
if j != 0 {
|
||||
value += dp[i][j - 1];
|
||||
}
|
||||
|
||||
if i != 0 || j != 0 {
|
||||
dp[i][j] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dp[m - 1][n - 1]
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_63() {
|
||||
assert_eq!(2, Solution::unique_paths_with_obstacles(vec![vec![0, 0, 0], vec![0, 1, 0], vec![0, 0, 0]]));
|
||||
assert_eq!(0, Solution::unique_paths_with_obstacles(vec![vec![1]]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user