-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathunique-paths-ii.rs
59 lines (57 loc) · 1.9 KB
/
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
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
// 63. Unique Paths II
// 🟠 Medium
//
// https://leetcode.com/problems/unique-paths-ii/
//
// Tags: Array - Dynamic Programming - Matrix
struct Solution;
impl Solution {
// Similar to unique paths, since we can only move right or down, the
// number of ways to reach any cell equals the sum of the number of ways
// to reach the cell above it and the number of ways to reach the cell to
// its left. When we find an obstacle, the number of ways to reach that
// cell is 0 since we cannot visit it.
//
// Time complexity: O(m*n) - Where m is the number of rows and n is the
// number of columns. We will visit each cell once.
// Space complexity: O(n) - We use an array of size n to store
// intermediate results.
//
// Runtime 1 ms Beats 88.57%
// Memory 2 MB Beats 94.29%
pub fn unique_paths_with_obstacles(obstacle_grid: Vec<Vec<i32>>) -> i32 {
let n = obstacle_grid[0].len();
// Use a one dimensional vector of length n + 1 to store
// intermediate results.
// Prefill a row before the initial row in the grid.
let mut dp = vec![0; n + 1];
dp[1] = 1;
for row in obstacle_grid {
for i in 1..dp.len() {
if row[i - 1] == 1 {
dp[i] = 0;
} else {
dp[i] += dp[i - 1];
}
}
}
*dp.last().unwrap()
}
}
// Tests.
fn main() {
assert_eq!(Solution::unique_paths_with_obstacles(vec![vec![0, 1]]), 0);
assert_eq!(
Solution::unique_paths_with_obstacles(vec![vec![0, 1], vec![0, 0]]),
1
);
assert_eq!(
Solution::unique_paths_with_obstacles(vec![vec![0, 0, 0], vec![0, 0, 0], vec![0, 0, 0]]),
6
);
assert_eq!(
Solution::unique_paths_with_obstacles(vec![vec![0, 0, 0], vec![0, 1, 0], vec![0, 0, 0]]),
2
);
println!("All tests passed!")
}