We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1a51455 commit 72b875bCopy full SHA for 72b875b
java/Dynamic Programming/MatrixPathways.java
@@ -0,0 +1,21 @@
1
+import java.util.Arrays;
2
+
3
+public class MatrixPathways {
4
+ public int matrixPathways(int m, int n) {
5
+ // Base cases: Set all cells in row 0 and column 0 to 1. We can
6
+ // do this by initializing all cells in the DP table to 1.
7
+ int[][] dp = new int[m][n];
8
+ for (int i = 0; i < m; i++) {
9
+ Arrays.fill(dp[i], 1);
10
+ }
11
+ // Fill in the rest of the DP table.
12
+ for (int r = 1; r < m; r++) {
13
+ for (int c = 1; c < n; c++) {
14
+ // Paths to current cell = paths from above + paths from
15
+ // left.
16
+ dp[r][c] = dp[r - 1][c] + dp[r][c - 1];
17
18
19
+ return dp[m - 1][n - 1];
20
21
+}
0 commit comments