-
Notifications
You must be signed in to change notification settings - Fork 7
/
329-Longest-Increasing-Path-in-a-Matrix.js
39 lines (31 loc) · 1.35 KB
/
329-Longest-Increasing-Path-in-a-Matrix.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
30
31
32
33
34
35
36
37
38
39
/**
* @param {number[][]} matrix
* @return {number}
*/
const longestIncreasingPath = (matrix) => {
let longest = 0;
const hash = {};
const traverse = (row, col) => {
if (row < 0 || col < 0 || row >= matrix.length || col >= matrix[row].length) return 0;
const key = `${row}-${col}`;
if (hash[key]) return hash[key];
const currentValue = matrix[row][col];
const upValue = matrix[row - 1] ? matrix[row - 1][col] : null;
const downValue = matrix[row + 1] ? matrix[row + 1][col] : null;
const leftValue = matrix[row][col - 1];
const rightValue = matrix[row][col + 1];
const upMaxValue = upValue && upValue > currentValue ? traverse(row - 1, col) : 0;
const downMaxValue = downValue && downValue > currentValue ? traverse(row + 1, col) : 0;
const leftMaxValue = leftValue && leftValue > currentValue ? traverse(row, col - 1) : 0;
const rightMaxValue = rightValue && rightValue > currentValue ? traverse(row, col + 1) : 0;
const result = Math.max(upMaxValue, downMaxValue, leftMaxValue, rightMaxValue) + 1;
hash[key] = result;
return result;
};
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
longest = Math.max(longest, traverse(i, j));
}
}
return longest;
};