-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNumberOfPaths.java
More file actions
39 lines (35 loc) · 842 Bytes
/
NumberOfPaths.java
File metadata and controls
39 lines (35 loc) · 842 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
/*https://practice.geeksforgeeks.org/problems/number-of-paths0926/1*/
class Solution{
long paths;
int row, col;
long numberOfPaths(int m, int n) {
paths = 0;
row = m;
col = n;
countPaths(0,0);
return paths;
}
public void countPaths(int r, int c)
{
if (r == row-1 && c == col-1) //when exhausted
{
++paths;
return;
}
//if reached at bottom, start moving right
if (r == row-1)
{
countPaths(r,c+1);
return;
}
//if reached at right end, start moving down
if (c == col-1)
{
countPaths(r+1,c);
return;
}
//in other cases, move in both the directions
countPaths(r+1,c);
countPaths(r,c+1);
}
}