1234. Number Of Paths With Max Score¶
Difficulty: Hard
LeetCode Problem View on GitHub
1234. Number of Paths with Max Score
Hard
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
Solution¶
import java.util.Arrays;
import java.util.List;
class Solution {
private int sum[][];
private int ways[][];
private int mod = (int)(1e9 + 7);
public int[] pathsWithMaxScore(List<String> board) {
int n = board.size();
int m = board.get(0).length();
char matrix[][] = new char[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
matrix[i][j] = board.get(i).charAt(j);
}
matrix[0][0] = '0';
matrix[n - 1][m - 1] = '0';
sum = new int[n + 1][m + 1];
ways = new int[n + 1][m + 1];
for (int current[] : sum)
Arrays.fill(current, Integer.MIN_VALUE / 10);
int dir[][] = {{0, -1}, {-1, 0}, {-1, -1}};
sum[n - 1][m - 1] = 0;
ways[n - 1][m - 1] = 1;
for (int i = n - 1; i >= 0; i--) {
for (int j = m - 1; j >= 0; j--) {
if (matrix[i][j] == 'X')
continue;
for (int dire[] : dir) {
int newRow = i + dire[0], newCol = j + dire[1];
if (newRow >= 0 && newRow < n && newCol >= 0 && newCol < m && matrix[newRow][newCol] != 'X') {
int newSum = sum[i][j] + matrix[newRow][newCol] - '0';
if (newSum > sum[newRow][newCol]) {
sum[newRow][newCol] = newSum;
ways[newRow][newCol] = ways[i][j];
} else if (newSum == sum[newRow][newCol])
ways[newRow][newCol] = (ways[newRow][newCol] + ways[i][j]) % mod;
}
}
}
}
if (ways[0][0] == 0)
return new int[] {0, 0};
return new int[] {sum[0][0], ways[0][0]};
}
}
Complexity Analysis¶
- Time Complexity:
O(?) - Space Complexity:
O(?)
Approach¶
Detailed explanation of the approach will be added here