1
var longestIncreasingPath = function (matrix) {2
const m = matrix.length,4
const dp = new Array(m).fill(0).map(() => {5
return new Array(n).fill(-1);9
const dir = [0, -1, 0, 1, 0];11
const dfs = (x, y) => {12
if (dp[x][y] != -1) return dp[x][y];14
for (let i = 1; i <= 4; i++) {15
const [nx, ny] = [x + dir[i], y + dir[i - 1]];21
matrix[nx][ny] > matrix[x][y]23
dp[x][y] = Math.max(dp[x][y], 1 + dfs(nx, ny));27
if (dp[x][y] == -1) dp[x][y] = 1;28
ans = Math.max(ans, dp[x][y]);32
for (let i = 0; i < m; i++) {33
for (let j = 0; j < n; j++) {34
if (dp[i][j] == -1) dfs(i, j);