-
Notifications
You must be signed in to change notification settings - Fork 7
/
417-Pacific-Atlantic-Water-Flow.js
60 lines (50 loc) · 1.65 KB
/
417-Pacific-Atlantic-Water-Flow.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
const explore = (h, row, col, rowLen, colLen, matrix, discovered) => {
if (row < 0 || col < 0) return 1;
if (row === rowLen || col === colLen) return 2;
if (h < matrix[row][col]) return 0;
if (discovered[row][col] === -Infinity) {
const ch = matrix[row][col];
discovered[row][col] = 0;
const result =
explore(ch, row - 1, col, rowLen, colLen, matrix, discovered) |
explore(ch, row + 1, col, rowLen, colLen, matrix, discovered) |
explore(ch, row, col - 1, rowLen, colLen, matrix, discovered) |
explore(ch, row, col + 1, rowLen, colLen, matrix, discovered);
discovered[row][col] = -Infinity;
return result;
}
return discovered[row][col];
};
/**
* @param {number[][]} matrix
* @return {number[][]}
*/
const pacificAtlantic = (matrix) => {
const result = [];
const rowLen = matrix.length;
if (rowLen === 0) return result;
const colLen = matrix[0].length;
if (colLen === 0) return result;
const discovered = [];
for (let row = 0; row < rowLen; row += 1) {
discovered[row] = [];
for (let col = 0; col < colLen; col += 1) {
discovered[row][col] = -Infinity;
}
}
for (let row = 0; row < rowLen; row += 1) {
for (let col = 0; col < colLen; col += 1) {
discovered[row][col] = explore(
matrix[row][col],
row,
col,
rowLen,
colLen,
matrix,
discovered
);
if (discovered[row][col] === 3) result.push([row, col]);
}
}
return result;
};