-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0063-unique-paths-ii.rs
29 lines (24 loc) · 1.08 KB
/
0063-unique-paths-ii.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
impl Solution {
pub fn unique_paths_with_obstacles(obstacleGrid: Vec<Vec<i32>>) -> i32 {
if obstacleGrid.is_empty() || obstacleGrid[0].is_empty() || obstacleGrid[0][0] == 1 {
return 0;
}
let m = obstacleGrid.len();
let n = obstacleGrid[0].len();
let mut previous = vec![0; n];
let mut current = vec![0; n];
previous[0] = 1;
for row in &obstacleGrid {
current[0] = if row[0] == 1 { 0 } else { previous[0] };
for j in 1..n {
if row[j] == 1 {
current[j] = 0;
} else {
current[j] = current[j-1] + previous[j];
}
}
std::mem::swap(&mut previous, &mut current);
}
previous[n-1]
}
}