-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0329-longest-increasing-path-in-a-matrix.js
More file actions
52 lines (40 loc) · 1.89 KB
/
0329-longest-increasing-path-in-a-matrix.js
File metadata and controls
52 lines (40 loc) · 1.89 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Longest Increasing Path In A Matrix
* Time Complexity: O(m * n)
* Space Complexity: O(m * n)
*/
var longestIncreasingPath = function (matrix) {
if (!matrix || matrix.length === 0 || matrix[0].length === 0) {
return 0;
}
const matrixHeight = matrix.length;
const matrixWidth = matrix[0].length;
const pathMemo = Array.from({ length: matrixHeight }, () => Array(matrixWidth).fill(0));
const directionOffsets = [[0, 1], [0, -1], [1, 0], [-1, 0]];
let maxOverallPath = 0;
const calculateLongestPath = (currentRowPosition, currentColPosition) => {
if (pathMemo[currentRowPosition][currentColPosition] !== 0) {
return pathMemo[currentRowPosition][currentColPosition];
}
let pathLengthFromHere = 1;
for (const directionPair of directionOffsets) {
const nextRowPosition = currentRowPosition + directionPair[0];
const nextColPosition = currentColPosition + directionPair[1];
const isValidRow = nextRowPosition >= 0 && nextRowPosition < matrixHeight;
const isValidCol = nextColPosition >= 0 && nextColPosition < matrixWidth;
if (isValidRow && isValidCol && matrix[nextRowPosition][nextColPosition] > matrix[currentRowPosition][currentColPosition]) {
const neighborPathResult = calculateLongestPath(nextRowPosition, nextColPosition);
pathLengthFromHere = Math.max(pathLengthFromHere, 1 + neighborPathResult);
}
}
pathMemo[currentRowPosition][currentColPosition] = pathLengthFromHere;
return pathLengthFromHere;
};
for (let rIdx = 0; rIdx < matrixHeight; rIdx++) {
for (let cIdx = 0; cIdx < matrixWidth; cIdx++) {
const currentPathValue = calculateLongestPath(rIdx, cIdx);
maxOverallPath = Math.max(maxOverallPath, currentPathValue);
}
}
return maxOverallPath;
};