Leetcode 1301. Number of Paths with Max Score
Description
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
自己写得dfs暴力求解超时了,这里参考lee215的解题思路,按照自己比较好理解的方式进行了适当的修改,大佬tql。
这里使用创建了3维的dp,其中 dp[i][j][0] 表示从右下角"S"到当前位置的最大和,dp[i][j][1]表示对应的路径数目。
dp状态更新过程如下:
- dp[i][j][0] = max(dp[i][j+1][0], dp[i+1][j][0], dp[i+1][j+1][0])
- 遍历(dp[i][j+1][0], dp[i+1][j][0], dp[i+1][j+1][0]),如果存在等于1中dp[i][j][0]的元素,那么dp[i][j][1]则加上对应元素第三维上第二个元素的值,即dp[x][y][1]的值,其中x,y表示[i][j]下方、右方、右下斜对角方的坐标。
class Solution:
def pathsWithMaxScore(self, board: List[str]) -> List[int]:
if not board:
return [0, 0]
n, mod = len(board), pow(10,9)+7
dp = [[[-1, 0] for _ in range(n+1)] for _ in range(n+1)]
dp[0][0] = [0, 0]
dp[n-1][n-1] = [0, 1]
for i in range(n)[::-1]:
for j in range(n)[::-1]:
if board[i][j] in 'XS':
continue
for mx, my in ((1,0), (0,1), (1,1)):
x, y = i+mx, j+my
if dp[i][j][0] < dp[x][y][0]:
dp[i][j] = [dp[x][y][0], 0]
if dp[i][j][0] == dp[x][y][0]:
dp[i][j][1] += dp[x][y][1]
dp[i][j][1] %= mod
# 如果不为终点且存在从右下角"S"到当前位置的路径,则更新到当前位置的最大和
if dp[i][j][0] != -1 and (i or j):
dp[i][j][0] += int(board[i][j])
dp[i][j][0] %= mod
return dp[0][0]