-
Notifications
You must be signed in to change notification settings - Fork 0
/
GridTraveller.java
53 lines (39 loc) · 1.33 KB
/
GridTraveller.java
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
/*
Problem Statement:
-----------------
The problem is to count all the possible paths from top left to bottom right of a mXn matrix with the constraints that from each cell you can either move only to right or down.
Examples :
---------
Input : row = 2, column = 2;
Output : 2
There are two paths
(0, 0) -> (0, 1) -> (1, 1)
(0, 0) -> (1, 0) -> (1, 1)
Input : row = 2, column = 3;
Output : 3
There are three paths
(0, 0) -> (0, 1) -> (0, 2) -> (1, 2)
(0, 0) -> (0, 1) -> (1, 1) -> (1, 2)
(0, 0) -> (1, 0) -> (1, 1) -> (1, 2)
The time complexity of this recursive solution is exponential.
We should use Dynamic Programming for linear time complexity.
*/
// Link --> https://www.geeksforgeeks.org/count-possible-paths-top-left-bottom-right-nxm-matrix/
public class GridTraveller {
public static int gridTraveller(int row, int column) {
// base cases:
if(row == 1 && column == 1) {
return 1;
}
if(row == 0 || column == 0) {
return 0;
}
return (gridTraveller(row-1, column) + gridTraveller(row, column - 1));
}
public static void main(String[] args) {
System.out.println(gridTraveller(1, 1));
System.out.println(gridTraveller(2, 3));
System.out.println(gridTraveller(3, 2));
System.out.println(gridTraveller(18, 18));
}
}