Skip to content

Commit 72b875b

Browse files
committed
add: MatrixPathways
1 parent 1a51455 commit 72b875b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)