Skip to content

Commit 1f7ab0b

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 63_Unique_Paths_II.java
1 parent b3c0c94 commit 1f7ab0b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
3+
if (obstacleGrid == null || obstacleGrid.length == 0) {
4+
return 0;
5+
}
6+
7+
int rows = obstacleGrid.length, cols = obstacleGrid[0].length;
8+
int[][] dp = new int[rows][cols];
9+
10+
for (int i = 0; i < rows; i++) {
11+
if (obstacleGrid[i][0] == 0) {
12+
dp[i][0] = 1;
13+
} else {
14+
while (i < rows) {
15+
dp[i][0] = 0;
16+
++i;
17+
}
18+
}
19+
}
20+
21+
for (int j = 0; j < cols; j++) {
22+
if (obstacleGrid[0][j] == 0) {
23+
dp[0][j] = 1;
24+
} else {
25+
while (j < cols) {
26+
dp[0][j] = 0;
27+
++j;
28+
}
29+
}
30+
}
31+
32+
for (int i = 1; i < rows; i++) {
33+
for (int j = 1; j < cols; j++) {
34+
if (obstacleGrid[i][j] == 1) {
35+
dp[i][j] = 0;
36+
continue;
37+
}
38+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
39+
}
40+
}
41+
42+
return dp[rows - 1][cols - 1];
43+
}
44+
}

0 commit comments

Comments
 (0)