Number of Paths with Max Score Problem
Description
LeetCode Problem 1301.
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
Approach: Dynamic Programming from S
Moves only go up, left or diagonally up-left, so every path from S to E passes through cells in a fixed order: a cell can only be reached from the cell below it, the cell to its right, or the cell diagonally below-right. That makes a bottom-up DP natural. Fill the board from the bottom-right corner towards the top-left, keeping two numbers for each cell (i, j):
best[i][j]: the highest score of a path fromSto this cell, or-1if no path reaches it.ways[i][j]: how many paths reach this cell with exactly that score, modulo 109 + 7.
Start with best = 0 and ways = 1 at S. For every other cell that is not an obstacle, look at its three predecessors:
- A predecessor with a higher
bestreplaces both numbers: the old paths are no longer the best ones. - A predecessor with the same
bestadds itsways: these are more paths with the same score.
Once the cell has a score, add its own digit (E and S count as 0). Padding the tables with an extra row and column of -1 keeps the boundary checks out of the loop.
The tricky part is telling "unreachable" apart from "score 0". If all predecessors are unreachable, the cell stays at -1 and never adds its digit, so its ways stays 0. At the end, best[0][0] = -1 means E cannot be reached, and the answer is [0, 0].
Complexity: O(n2) time and O(n2) space for an n x n board.
class Solution:
def pathsWithMaxScore(self, board: List[str]) -> List[int]:
MOD = 10**9 + 7
n = len(board)
# best[i][j]: the highest score on a path from S to (i, j), or -1 if (i, j) cannot be reached.
# ways[i][j]: how many paths reach (i, j) with that score. Row n and column n are padding.
best = [[-1] * (n + 1) for _ in range(n + 1)]
ways = [[0] * (n + 1) for _ in range(n + 1)]
best[n - 1][n - 1], ways[n - 1][n - 1] = 0, 1
for i in range(n - 1, -1, -1):
for j in range(n - 1, -1, -1):
if board[i][j] in "XS":
continue
# We arrive from below, from the right, or diagonally from below-right.
for x, y in ((i + 1, j), (i, j + 1), (i + 1, j + 1)):
if best[x][y] > best[i][j]:
best[i][j], ways[i][j] = best[x][y], ways[x][y]
elif best[x][y] == best[i][j]:
ways[i][j] = (ways[i][j] + ways[x][y]) % MOD
if best[i][j] >= 0 and board[i][j] != "E":
best[i][j] += int(board[i][j])
if best[0][0] < 0:
return [0, 0]
return [best[0][0], ways[0][0]]