-
Notifications
You must be signed in to change notification settings - Fork 1
/
NumberOfPathsInMxNMatrix.java
68 lines (60 loc) · 1.7 KB
/
NumberOfPathsInMxNMatrix.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package datastructure.matrix.program;
public class NumberOfPathsInMxNMatrix {
public int countPathsDP(int n, int m, int[][] result) {
if (n == 1 || m == 1) {
return 1;
}
if (result[n - 1][m - 1] != 0)
return result[n - 1][m - 1];
result[n - 1][m - 1] = countPathsDP(n - 1, m, result) + countPathsDP(n, m - 1, result);
return result[n - 1][m - 1];
}
/**
* Recursive method
*
* @param n
* @param m
* @return
*/
public int countPathsRecursive(int n, int m) {
if (n == 1 || m == 1) {
return 1;
}
return countPathsRecursive(n - 1, m) + countPathsRecursive(n, m - 1);
}
/**
* Iterative method
*
* @param n
* @param m
* @return
*/
public int countPaths(int n, int m) {
int T[][] = new int[n][m];
for (int i = 0; i < n; i++) {
T[i][0] = 1;
}
for (int i = 0; i < m; i++) {
T[0][i] = 1;
}
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
T[i][j] = T[i - 1][j] + T[i][j - 1];
}
}
return T[n - 1][m - 1];
}
public static void main(String args[]) {
NumberOfPathsInMxNMatrix nop = new NumberOfPathsInMxNMatrix();
int[][] result = new int[1000][1000];
long start = System.currentTimeMillis();
System.out.println(nop.countPathsDP(1000, 1000, result));
System.out.println(System.currentTimeMillis() - start);
start = System.currentTimeMillis();
System.out.println(nop.countPaths(1000, 1000));
System.out.println(System.currentTimeMillis() - start);
start = System.currentTimeMillis();
System.out.println(nop.countPathsRecursive(1000, 1000));
System.out.println(System.currentTimeMillis() - start);
}
}