1. Problem Understanding
We are given an n × n maze where every cell contains one of three values:
1→ Move Right only2→ Move Down only3→ Move Right or Down
We start at (0, 0) and need to reach (n-1, n-1).
For every valid path, we need to calculate two things:
Total number of valid paths
Maximum Adventure, where Adventure is the sum of all cell values visited in the path.
The result should be:
[totalPaths, maxAdventure]
The number of paths must be calculated modulo 10^9 + 7.
2. Why Dynamic Programming?
A brute-force solution would try every possible path from the starting cell.
That becomes inefficient because the number of possible Right/Down combinations can grow very quickly.
For example, if every cell allows both directions, there can be a large number of paths.
However, notice an important property:
To reach a cell
(i, j), we can only come from(i-1, j)or(i, j-1).
Therefore, instead of calculating the same information repeatedly, we can store the answer for every cell.
This is exactly what Dynamic Programming (DP) is useful for.
3. DP State
We maintain two DP arrays.
ways[i][j]
This stores:
The number of valid paths from
(0,0)to(i,j).
long[][] ways = new long[n][n];
For example:
ways[i][j] = 5
means there are 5 different valid ways to reach (i,j).
maxSum[i][j]
This stores:
The maximum Adventure possible among all valid paths from
(0,0)to(i,j).
int[][] maxSum = new int[n][n];
For example:
maxSum[i][j] = 15
means the highest Adventure among the paths reaching (i,j) is 15.
4. Starting Cell
We begin at (0,0).
There is exactly one way to reach the starting cell: we are already there.
Therefore:
ways[0][0] = 1;
The Adventure is simply the value of the starting cell:
maxSum[0][0] = grid[0][0];
So:
ways[0][0] = 1;
maxSum[0][0] = grid[0][0];
5. Understanding the Movement Rules
The most important part of the solution is understanding how a cell can be reached.
Suppose we want to reach:
(i, j)
There are only two possible previous cells:
(i-1, j) → from above
(i, j-1) → from left
But the previous cell must allow the required movement.
6. Coming From Above
Suppose we are moving:
(i-1, j)
↓
(i, j)
The previous cell must allow Down movement.
A cell allows Down when its value is:
2 → Down
3 → Right or Down
Therefore:
if (grid[i - 1][j] == 2 || grid[i - 1][j] == 3)
If this condition is true, all valid paths reaching (i-1,j) can continue to (i,j).
Therefore:
count = (count + ways[i - 1][j]) % MOD;
For the maximum Adventure:
maxSum[i - 1][j] + grid[i][j]
We add the current cell's value because the current cell is also part of the path.
7. Coming From the Left
Now suppose we move:
(i, j-1) → (i, j)
The previous cell must allow Right movement.
A cell allows Right when its value is:
1 → Right
3 → Right or Down
Therefore:
if (grid[i][j - 1] == 1 || grid[i][j - 1] == 3)
If valid, we add the number of paths:
count = (count + ways[i][j - 1]) % MOD;
And calculate the possible maximum Adventure:
maxSum[i][j - 1] + grid[i][j]
8. Complete Java Code
import java.util.*;
class Solution {
static final int MOD = 1000000007;
public ArrayList<Integer> findWays(int[][] grid) {
int n = grid.length;
long[][] ways = new long[n][n];
int[][] maxSum = new int[n][n];
// Starting cell
ways[0][0] = 1;
maxSum[0][0] = grid[0][0];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Starting cell is already initialized
if (i == 0 && j == 0) {
continue;
}
long count = 0;
int best = -1;
// Check cell from above
if (i > 0 &&
(grid[i - 1][j] == 2 ||
grid[i - 1][j] == 3)) {
if (ways[i - 1][j] > 0) {
count = (count + ways[i - 1][j]) % MOD;
best = Math.max(
best,
maxSum[i - 1][j] + grid[i][j]
);
}
}
// Check cell from left
if (j > 0 &&
(grid[i][j - 1] == 1 ||
grid[i][j - 1] == 3)) {
if (ways[i][j - 1] > 0) {
count = (count + ways[i][j - 1]) % MOD;
best = Math.max(
best,
maxSum[i][j - 1] + grid[i][j]
);
}
}
ways[i][j] = count;
if (count > 0) {
maxSum[i][j] = best;
}
}
}
ArrayList<Integer> result = new ArrayList<>();
result.add((int) ways[n - 1][n - 1]);
if (ways[n - 1][n - 1] == 0) {
result.add(0);
} else {
result.add(maxSum[n - 1][n - 1]);
}
return result;
}
}

Join the conversation! Your thoughts help the community grow.