1
# Runtime: 321 ms (Top 53.09%) | Memory: 26.5 MB (Top 8.64%)3
def pathsWithMaxScore(self, board: List[str]) -> List[int]:5
## Basic information: height and width of board6
h, w = len(board), len(board[0])8
# ----------------------------------------------------------------------------10
# Use pathon native cahce as memoization for DP14
if i == h - 1 and j == w - 1:17
# score for start coordinate = 018
# paht count for start coordinate = 121
elif i >= w or j >= h or board[i][j] == "X":24
# Out-of-boundary, or meet obstacle28
# update from three possible preivous moves from right, down, and diagonal30
right_score, right_path_count = visit(i, j + 1)31
down_score, down_path_count = visit(i + 1, j)32
diag_score, diag_path_count = visit(i + 1, j + 1)34
max_prevScore = max(right_score, down_score, diag_score)37
cur_score = int(board[i][j]) if board[i][j] != "E" else 039
if right_score == max_prevScore:40
cur_path_count += right_path_count41
if down_score == max_prevScore:42
cur_path_count += down_path_count43
if diag_score == max_prevScore:44
cur_path_count += diag_path_count46
return max_prevScore + cur_score, cur_path_count48
# ----------------------------------------------------------------------------50
## Remark: Remember to take modulo by constant, this is defined by description52
maxScore, validPathCount = visit(0, 0)54
return [maxScore % CONST, validPathCount % CONST] if validPathCount else [0, 0]