Skip to content

Commit 4cd32ea

Browse files
authored
Create 63. Unique Path II
1 parent 0ddf51c commit 4cd32ea

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

63. Unique Path II

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
public class Solution {
2+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
3+
// int m = obstacleGrid.length;
4+
// int n = obstacleGrid[0].length;
5+
// // use dp
6+
7+
// Integer[][] map = new Integer[m][n];
8+
9+
// if(obstacleGrid[0][0] == 1){
10+
// return 0;
11+
// }else{
12+
// map[0][0] = 1;
13+
// }
14+
15+
// for(int i = 1; i<m;i++){
16+
// if(map[i-1][0] != 0){
17+
// map[i][0] = 1;
18+
// }
19+
// if(obstacleGrid[i][0] == 1){
20+
// map[i][0] = 0;
21+
// }
22+
// }
23+
// for(int j= 1;j<n;j++){
24+
// if(map[0][j-1] != 0){
25+
// map[0][j] = 1;
26+
// }
27+
// if(obstacleGrid[0][j] == 1){
28+
// map[0][j] = 0;
29+
// }
30+
// }
31+
// for(int i = 1;i < m;i++){
32+
// for(int j = 1; j < n;j++){
33+
// if(i != 0 && j!= 0)
34+
// map[i][j] = map[i-1][j] + map[i][j-1];
35+
// if(obstacleGrid[i][j] == 1){
36+
// map[i][j] = 0;
37+
// }
38+
// }
39+
// }
40+
// return map[m-1][n-1];
41+
42+
//Empty case
43+
if(obstacleGrid.length == 0) return 0;
44+
45+
int rows = obstacleGrid.length;
46+
int cols = obstacleGrid[0].length;
47+
48+
for(int i = 0; i < rows; i++){
49+
for(int j = 0; j < cols; j++){
50+
if(obstacleGrid[i][j] == 1)
51+
obstacleGrid[i][j] = 0;
52+
else if(i == 0 && j == 0)
53+
obstacleGrid[i][j] = 1;
54+
else if(i == 0)
55+
obstacleGrid[i][j] = obstacleGrid[i][j - 1] * 1;// For row 0, if there are no paths to left cell, then its 0,else 1
56+
else if(j == 0)
57+
obstacleGrid[i][j] = obstacleGrid[i - 1][j] * 1;// For col 0, if there are no paths to upper cell, then its 0,else 1
58+
else
59+
obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1];
60+
}
61+
}
62+
63+
return obstacleGrid[rows - 1][cols - 1];
64+
65+
}
66+
}

0 commit comments

Comments
 (0)