-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0063_unique_paths_ii.rs
More file actions
61 lines (54 loc) · 1.45 KB
/
s0063_unique_paths_ii.rs
File metadata and controls
61 lines (54 loc) · 1.45 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#![allow(unused)]
pub struct Solution {}
impl Solution {
pub fn unique_paths_with_obstacles(mut obstacle_grid: Vec<Vec<i32>>) -> i32 {
let (m, n) = (obstacle_grid.len(), obstacle_grid[0].len());
if obstacle_grid[0][0] == 1 {
return 0;
}
obstacle_grid[0][0] = 1;
for i in 1..m {
obstacle_grid[i][0] = if obstacle_grid[i][0] == 0 && obstacle_grid[i - 1][0] == 1 {
1
} else {
0
};
}
for j in 1..n {
obstacle_grid[0][j] = if obstacle_grid[0][j] == 0 && obstacle_grid[0][j - 1] == 1 {
1
} else {
0
};
}
for i in 1..m {
for j in 1..n {
if obstacle_grid[i][j] == 0 {
obstacle_grid[i][j] = obstacle_grid[i - 1][j] + obstacle_grid[i][j - 1];
} else {
obstacle_grid[i][j] = 0;
}
}
}
obstacle_grid[m - 1][n - 1]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_63() {
assert_eq!(
Solution::unique_paths_with_obstacles(vec![
vec![0, 0, 0],
vec![0, 1, 0],
vec![0, 0, 0]
]),
2
);
assert_eq!(
Solution::unique_paths_with_obstacles(vec![vec![0, 1], vec![0, 0]]),
1
);
}
}