Number of Paths with Max Score
You are given a square board
of characters. You can move on the board starting at the bottom right square marked with the character 'S'
.
You need to reach the top left square marked with the character 'E'
. The rest of the squares are labeled either with a numeric character 1, 2, ..., 9
or with an obstacle 'X'
. In one move you can go up, left or up-left (diagonally) only if there is no obstacle there.
Return a list of two integers: the first integer is the maximum sum of numeric characters you can collect, and the second is the number of such paths that you can take to get that maximum sum, taken modulo 10^9 + 7
.
In case there is no path, return [0, 0]
.
Example 1:
Input: board = ["E23","2X2","12S"]
Output: [7,1]
Example 2:
Input: board = ["E12","1X1","21S"]
Output: [4,2]
Example 3:
Input: board = ["E11","XXX","11S"]
Output: [0,0]
Constraints:
2 <= board.length == board[i].length <= 100
class Solution {
public int[] pathsWithMaxScore(List<String> board) {
int m = board.size(), n = board.get(0).length();
int[][] dpSum = new int[m][n];
int[][] dpPaths = new int[m][n];
int[][] DIRS = new int[][] { { 0, -1 }, { -1, 0 }, { -1, -1 } };
dpPaths[m - 1][n - 1] = 1; // start at the bottom right square
for (int r = m - 1; r >= 0; r--) {
for (int c = n - 1; c >= 0; c--) {
if (dpPaths[r][c] == 0)
continue; // can't reach to this square
for (int[] dir : DIRS) {
int nr = r + dir[0], nc = c + dir[1];
if (nr >= 0 && nc >= 0 && board.get(nr).charAt(nc) != 'X') {
int nsum = dpSum[r][c];
// If it is not 'E', then we can add digit to it's sum
if (board.get(nr).charAt(nc) != 'E')
nsum += board.get(nr).charAt(nc) - '0';
// If this sum > that the sum that position holds
// then total paths will be equal to current paths of dp[r][c] only
if (nsum > dpSum[nr][nc]) {
dpPaths[nr][nc] = dpPaths[r][c];
dpSum[nr][nc] = nsum;
}
// If sum is same to previous sum it holds, then we can just add our paths to it
else if (nsum == dpSum[nr][nc])
dpPaths[nr][nc] = (dpPaths[nr][nc] + dpPaths[r][c]) % 1000000007;
}
}
}
}
return new int[] { dpSum[0][0], dpPaths[0][0] };
}
}
Last updated