-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy path63. Unique Paths II
78 lines (72 loc) · 2.38 KB
/
63. Unique Paths II
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//Basic DFS : TLE as time complexity goes O(2^(M*N))
class Solution {
int un=0;
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
dfs(obstacleGrid,0,0);
return un;
}
void dfs(int[][] obstacleGrid,int row,int col){
if(row==obstacleGrid.length-1 && col==obstacleGrid[0].length-1 && obstacleGrid[row][col]==0){
un++;
return;
}
if(row<0 || col<0 || row==obstacleGrid.length || col==obstacleGrid[0].length || obstacleGrid[row][col]==1){
return;
}
dfs(obstacleGrid,row,col+1);
dfs(obstacleGrid,row+1,col);
}
}
//Adding Memoization for above code
class Solution {
int[][] dp;
public int uniquePathsWithObstacles(int[][] grid) {
dp = new int[grid.length][grid[0].length];
for(int[] d : dp) {
Arrays.fill(d, -1);
}
return dfs(grid, 0, 0);
}
int dfs(int[][] grid, int row, int col) {
//Exit condition
if(row < 0 || col < 0 || row == grid.length || col == grid[0].length || grid[row][col] == 1)
return 0;
if(row == grid.length-1 && col == grid[0].length-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];
}
}
//Top Down Approach
class Solution {
public int uniquePathsWithObstacles(int[][] grid) {
int m = grid.length, n = grid[0].length;
if(grid[0][0] == 1) return 0;
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
//First row && first column
if(i == 0 || j ==0) {
//its prev cell was obstacle or if current cell is obstacle
if(grid[i][j] == 1 || (i!=0 && grid[i-1][0] == 0) || (j!=0 && grid[i][j-1] == 0)) {
grid[i][j] = 0;
}
else {
grid[i][j] = 1;
}
}
//All the other cells
else {
if(grid[i][j] == 1) {
grid[i][j] = 0;
}
else {
grid[i][j] = grid[i-1][j] + grid[i][j-1];
}
}
}
}
return grid[m-1][n-1];
}
}