-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINTERVIEWBITPATH.java
More file actions
37 lines (29 loc) · 1.01 KB
/
INTERVIEWBITPATH.java
File metadata and controls
37 lines (29 loc) · 1.01 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
import java.util.*;
public class Solution {
public int uniquePathsWithObstacles(ArrayList<ArrayList<Integer>> A) {
int[][] grid = new int[A.size()][A.get(0).size()];
for (int i = 0; i < A.size(); i++) {
for (int j = 0; j < A.get(0).size(); j++) {
grid[i][j] = A.get(i).get(j);
}
}
int n = grid.length;
int m = grid[0].length;
if (grid[0][0] == 1 || grid[n - 1][m - 1] == 1) return 0;
int[][] dp = new int[n][m];
for (int[] row : dp) {
Arrays.fill(row, -1);
}
return helper(n - 1, m - 1, grid, dp);
}
public static int helper(int i, int j, int[][] grid, int[][] dp) {
if (i < 0 || j < 0) return 0;
if (grid[i][j] == 1) return 0;
if (i == 0 && j == 0) return 1;
if (dp[i][j] != -1) return dp[i][j];
int up = helper(i - 1, j, grid, dp);
int left = helper(i, j - 1, grid, dp);
dp[i][j] = up + left;
return dp[i][j];
}
}