-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution.js
29 lines (27 loc) · 848 Bytes
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* @param {number[][]} grid
* @return {number}
*/
var maxMoves = function (grid) {
const m = grid.length,
n = grid[0].length;
const dp = Array.from({ length: m }, () => Array(n).fill(0));
let maxMoves = 0;
for (let col = n - 2; col >= 0; col--) {
for (let row = 0; row < m; row++) {
if (row > 0 && grid[row][col] < grid[row - 1][col + 1]) {
dp[row][col] = Math.max(dp[row][col], dp[row - 1][col + 1] + 1);
}
if (grid[row][col] < grid[row][col + 1]) {
dp[row][col] = Math.max(dp[row][col], dp[row][col + 1] + 1);
}
if (row < m - 1 && grid[row][col] < grid[row + 1][col + 1]) {
dp[row][col] = Math.max(dp[row][col], dp[row + 1][col + 1] + 1);
}
}
}
for (let row = 0; row < m; row++) {
maxMoves = Math.max(maxMoves, dp[row][0]);
}
return maxMoves;
};