Number of Paths with Max Score LeetCode solution in JavaScript

1
var pathsWithMaxScore = function (board) {
2
let n = board.length,
3
dp = Array(n + 1)
4
.fill(0)
5
.map(() =>
6
Array(n + 1)
7
.fill(0)
8
.map(() => [-Infinity, 0])
9
);
10
let mod = 10 ** 9 + 7;
11
dp[n - 1][n - 1] = [0, 1]; // [max score, number of paths]
12

13
for (let i = n - 1; i >= 0; i--) {
14
for (let j = n - 1; j >= 0; j--) {
15
if (board[i][j] === "X" || board[i][j] === "S") continue;
16

17
let paths = [dp[i][j + 1], dp[i + 1][j + 1], dp[i + 1][j]];
18
for (let [maxScore, numPaths] of paths) {
19
if (dp[i][j][0] < maxScore) {
20
dp[i][j] = [maxScore, numPaths];
21
} else if (dp[i][j][0] === maxScore) {
22
dp[i][j][1] = (dp[i][j][1] + numPaths) % mod;
23
}
24
}
25
let score = board[i][j] === "E" ? 0 : Number(board[i][j]);
26
dp[i][j][0] += score;
27
}
28
}
29
return dp[0][0][1] === 0 ? [0, 0] : dp[0][0];
30
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0