-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathGridPathFinder.java
More file actions
29 lines (24 loc) · 805 Bytes
/
GridPathFinder.java
File metadata and controls
29 lines (24 loc) · 805 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
package dp_multidimensional;
public class GridPathFinder {
public int uniquePaths(int m, int n) {
Integer[][] map = new Integer[m][n];
for (int i = 0; i < m; i++) {
map[i][0] = 1;
}
for (int j = 0; j < n; j++) {
map[0][j] = 1;
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
map[i][j] = map[i - 1][j] + map[i][j - 1];
}
}
return map[m - 1][n - 1];
}
public static void main(String[] args) {
GridPathFinder finder = new GridPathFinder();
assert finder.uniquePaths(3, 7) == 28 : "Test case 1 failed";
assert finder.uniquePaths(3, 2) == 3 : "Test case 2 failed";
System.out.println("All test cases passed!");
}
}