-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.java
More file actions
33 lines (23 loc) · 766 Bytes
/
1.java
File metadata and controls
33 lines (23 loc) · 766 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
31
32
33
// Q1.Unique paths 2
class Solution {
int[][] dp;
public int uniquePathsWithObstacles(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
dp = new int[m][n];
for (int i = 0; i < m; i++)
Arrays.fill(dp[i], -1);
return dfs(grid, 0, 0);
}
int dfs(int[][] grid, int row, int col) {
int m = grid.length, n = grid[0].length;
if (row >= m || col >= n || grid[row][col] == 1)
return 0;
if (row == m - 1 && col == n - 1)
return 1;
if (dp[row][col] != -1)
return dp[row][col];
dp[row][col] = dfs(grid, row, col + 1) + dfs(grid, row + 1, col);
return dp[row][col];
}
}