forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0063-unique-paths-ii.java
More file actions
30 lines (27 loc) · 836 Bytes
/
0063-unique-paths-ii.java
File metadata and controls
30 lines (27 loc) · 836 Bytes
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
//Same as unique paths 1 just one extra condition to return 0 if grid[i][j] == 1
class Solution {
public int uniquePathsWithObstacles(int[][] grid) {
return dfs(
grid,
0,
0,
grid.length,
grid[0].length,
new int[grid.length][grid[0].length]
);
}
public int dfs(int[][] grid, int i, int j, int m, int n, int[][] dp) {
if (i < 0 || j < 0 || i >= m || j >= n || grid[i][j] == 1) {
return 0;
}
if (i == m - 1 && j == n - 1) {
dp[i][j] = 1;
return dp[i][j];
}
if (dp[i][j] != 0) return dp[i][j];
int right = dfs(grid, i, j + 1, m, n, dp);
int left = dfs(grid, i + 1, j, m, n, dp);
dp[i][j] = right + left;
return dp[i][j];
}
}