Skip to content

Commit 5683076

Browse files
committed
Create solution for Unique Path II
1 parent 90d0f7c commit 5683076

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

DP Problems/Unique-Paths-II.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int uniquePathsWithObstacles(int[][] grid) {
3+
if (grid[0][0] == 1) {
4+
return 0;
5+
}
6+
grid[0][0] = 1;
7+
for (int i = 1; i < grid.length; i++) {
8+
grid[i][0] = (grid[i][0] == 0 && grid[i-1][0] == 1) ? 1 : 0;
9+
}
10+
for (int j = 1; j < grid[0].length; j++) {
11+
grid[0][j] = (grid[0][j] == 0 && grid[0][j-1] == 1) ? 1 : 0;
12+
}
13+
14+
for (int n = 1; n < grid.length; n++) {
15+
for (int m = 1; m < grid[n].length; m++) {
16+
grid[n][m] = (grid[n][m] != 1) ? (grid[n-1][m] + grid[n][m-1]) : 0;
17+
}
18+
}
19+
return grid[grid.length-1][grid[0].length-1];
20+
}
21+
}

0 commit comments

Comments
 (0)