-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoofpathsinamatrix.java
More file actions
41 lines (31 loc) · 871 Bytes
/
Copy pathNoofpathsinamatrix.java
File metadata and controls
41 lines (31 loc) · 871 Bytes
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
/*
* Complete the function below.
*/
static int numberOfPaths(int[][] a) {
int c = a[0].length;
int r = a.length;
if(a[0][0] == 0 ||a[r-1][c-1]==0 )
{
return 0;
}
return countWays(a,r,c);
}
private static int countWays(int[][] maze, int r,int c) {
int dp[][] = new int[r][c];
dp[0][0] = 1;
for(int i = 1;i<r;i++){
dp[i][0] = maze[i][0]!=0? dp[i-1][0]:0;
}
for(int i = 1;i<c;i++){
dp[0][i] = maze[0][i]!=0? dp[0][i-1]:0;
}
for(int i=1;i<r;i++){
for(int j=1;j<c;j++){
if(maze[i][j]==0)
dp[i][j] = 0;
else
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[r-1][c-1];
}