-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUniquePathsIi.java
84 lines (78 loc) · 1.88 KB
/
UniquePathsIi.java
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Link: https://leetcode.com/problems/unique-paths-ii/
// This problem is the follow up of 62-Unique-Paths (../62-Unique-Paths/UniquePaths.java)
/*
* DP
* 0ms, 100%
*/
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
// Corner case: if the goal cell has obstacle, no valid paths in this grid, return 0 directly
if (obstacleGrid[m - 1][n - 1] == 1) {
return 0;
}
// M[i][j] represents how many unique paths from grid[i][j] to the goal
int[][] M = new int[m][n];
M[m - 1][n - 1] = 1;
// Induction rule:
// M[i][j] = 0 if grid[i][j] has obstacle
// = M[i+1][j] + M[i][j+1] otherwise
for (int i = m - 1; i >= 0; --i) {
for (int j = n - 1; j >= 0; --j) {
if (obstacleGrid[i][j] == 1) {
continue;
}
if (i + 1 < m) {
M[i][j] += M[i + 1][j];
}
if (j + 1 < n) {
M[i][j] += M[i][j + 1];
}
}
}
return M[0][0];
}
}
/*
* Time complexity: O(m*n)
*
* Space complexity: O(m*n)
*
* NOTES:
* The 'Final' cell may have obstacle as well. If so, there will be no paths for this grid, 0 should be
* returned.
*/
/*
* Method-2: DP - optimize the space complexity to 1D array
* 0ms, 100%
*/
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
if (obstacleGrid[m - 1][n - 1] == 1) {
return 0;
}
int[] M = new int[n];
M[n - 1] = 1;
for (int i = m - 1; i >= 0; --i) {
for (int j = n - 1; j >= 0; --j) {
if (obstacleGrid[i][j] == 1) {
M[j] = 0;
} else if (j + 1 < n) {
M[j] += M[j + 1];
}
}
}
return M[0];
}
}
/*
* Time complexity: O(m*n)
*
* Space complexity: O(n)
*
* NOTES:
*
*/