forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0329-longest-increasing-path-in-a-matrix.js
176 lines (138 loc) · 6.2 KB
/
0329-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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Brute Force - DFS
* Time O(2^(N + M)) | Space O(N * M)
* https://leetcode.com/problems/longest-increasing-path-in-a-matrix/
* @param {number[][]} matrix
* @return {number}
*/
var longestIncreasingPath = (matrix, maxPath = 0) => {
const [ rows, cols ] = [ matrix.length, matrix[0].length ];
for (let row = 0; (row < rows); row++) {/* Time O(N) */
for (let col = 0; (col < cols); col++) {/* Time O(M) */
const path = dfs(matrix, row, rows, col, cols);/* Time O(2^(N + M)) | Space O(HEIGHT) */
maxPath = Math.max(maxPath, path);
}
}
return maxPath;
}
var dfs = (matrix, row, rows, col, cols, ans = 0) => {
for (const [ _row, _col ] of getNeighbors(row, rows, col, cols)) {/* Time O(4) */
const path = dfs(matrix, _row, rows, _col, cols); /* Time O(2^(N + M)) | Space O(HEIGHT) */
ans = Math.max(ans, path);
}
ans += 1;
return ans;
}
var getNeighbors = (row, rows, col, cols) => [ [ 0, 1 ], [ 0, -1 ], [ 1, 0 ], [ -1, 0 ] ]
.map(([ _row, _col ]) => [ (row + _row), (col + _col) ])
.filter(([ _row, _col ]) => (0 <= _row) && (_row < rows) && (0 <= _col) && (_col < cols));
/**
* DP - Top Down
* Matrix - Memoization
* Time O(N * M) | Space O(N * M)
* https://leetcode.com/problems/longest-increasing-path-in-a-matrix/
* @param {number[][]} matrix
* @return {number}
*/
var longestIncreasingPath = (matrix, maxPath = 0, memo = initMemo(matrix)) => {
const [ rows, cols ] = [ matrix.length, matrix[0].length ];
for (let row = 0; row < rows; row++) {/* Time O(N) */
for (let col = 0; col < cols; col++) {/* Time O(M) */
const path = /* Time O(N * M) | Space O((N * M) + HEIGHT) */
search(matrix, row, rows, col, cols, memo);
maxPath = Math.max(maxPath, path);
}
}
return maxPath;
};
var initMemo = (matrix) => new Array(matrix.length).fill()/* Time O(N) | Space O(N)*/
.map(() => new Array(matrix[0].length).fill(0)); /* Time O(M) | Space O(M)*/
const search = (matrix, row, rows, col, cols, memo) => {
const hasSeen = (memo[row][col] !== 0)
if (hasSeen) return memo[row][col];
return dfs(matrix, row, rows, col, cols, memo);/* Time O(N * M) | Space O((N * M) + HEIGHT) */
}
var dfs = (matrix, row, rows, col, cols, memo) => {
for (const [ _row, _col ] of getNeighbors(row, rows, col, cols)) {/* Time O(4) */
const [ parent, node ] = [ matrix[row][col], matrix[_row][_col] ];
const isLess = (node <= parent);
if (isLess) continue;
const path = search(matrix, _row, rows, _col, cols, memo); /* Time O(N * M) | Space O(HEIGHT) */
memo[row][col] = Math.max(memo[row][col], path);
}
memo[row][col] += 1; /* | Space O(N * M) */
return memo[row][col];
}
var getNeighbors = (row, rows, col, cols) => [ [ 0, 1 ], [ 0, -1 ], [ 1, 0 ], [ -1, 0 ] ]
.map(([ _row, _col ]) => [ (row + _row), (col + _col) ])
.filter(([ _row, _col ]) => (0 <= _row) && (_row < rows) && (0 <= _col) && (_col < cols));
/**
* Topological Sort
* Matrix - Graph
* Matrix - In-Degree
* Queue - BFS
* Time O(N * M) | Space O(N * M)
* https://leetcode.com/problems/longest-increasing-path-in-a-matrix/
* @param {number[][]} matrix
* @return {number}
*/
var longestIncreasingPath = (matrix) => {
const { graph, indegree, sources } = /* Time O(N * M) | Space O(N * M) */
buildGraph(matrix);
findSources(graph, indegree, sources);/* Time O(N * M) | Space O(N * M) */
return bfs(graph, indegree, sources); /* Time O((N * M) + WIDTH) | Space O((N * M) + WIDTH) */
}
const initGraph = (rows, cols) => ({
graph: new Array((rows + 2)).fill() /* Time O(N) | Space O(N) */
.map(() => new Array((cols + 2)).fill(0)),/* Time O(M) | Space O(M) */
indegree: new Array((rows + 2)).fill() /* Time O(N) | Space O(N) */
.map(() => new Array((cols + 2)).fill(0)),/* Time O(M) | Space O(M) */
sources: new Queue()
})
var buildGraph = (matrix) => {
const [ rows, cols ] = [ matrix.length, matrix[0].length ];
const { graph, indegree, sources } = /* Time O(N * M) | Space O(N * M) */
initGraph(rows, cols);
for (let row = 1; (row < (rows + 1)); row++) {/* Time O(N) */
graph[row] = [ 0, ...matrix[row - 1], 0 ]; /* | Space O(N * M) */
}
for (let row = 1; (row <= rows); row++) { /* Time O(N) */
for (let col = 1; (col <= cols); col++) { /* Time O(M) */
for (const [ _row, _col ] of getNeighbors(row, col)) {/* Time O(4) */
const isSink = (graph[row][col] < graph[_row][_col]);
if (isSink) indegree[row][col] += 1; /* | Space O(N * M) */
}
}
}
return { graph, indegree, sources };
}
var getNeighbors = (row, col) => [ [ 0, 1 ], [ 0, -1 ], [ 1, 0 ], [ -1, 0 ] ]
.map(([ _row, _col ]) => [ (row + _row), (col + _col) ]);
var findSources = (graph, indegree, sources) => {
const [ rows, cols ] = [ graph.length, graph[0].length ];
for (let row = 1; (row < (rows - 1)); ++row) {/* Time O(N) */
for (let col = 1; (col < (cols - 1)); ++col) {/* Time O(M) */
const isSource = (indegree[row][col] === 0);
if (isSource) sources.enqueue([ row, col ]); /* Space O(N * M) */
}
}
}
const bfs = (graph, indegree, sources, path = 0) => {
while (!sources.isEmpty()) { /* Time(N * M) */
for (let level = (sources.size() - 1); 0 <= level; level--) {/* Time(WIDTH) */
checkNeighbors(graph, indegree, sources) /* Space((N * M) + WIDTH) */
}
path += 1;
}
return path;
}
const checkNeighbors = (graph, indegree, sources) => {
const [ row, col ] = sources.dequeue();
for (const [ _row, _col ] of getNeighbors(row, col)) {
const canDisconnect = (graph[_row][_col] < graph[row][col]);
if (!canDisconnect) continue;
indegree[_row][_col] -= 1; /* Space O(N * M) */
const isSource = (indegree[_row][_col] === 0);
if (isSource) sources.enqueue([ _row, _col ]);/* Space O(WIDTH) */
}
}