forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap of Highest Peak.js
32 lines (29 loc) · 930 Bytes
/
Map of Highest Peak.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
// Runtime: 929 ms (Top 36.36%) | Memory: 127.8 MB (Top 45.45%)
var highestPeak = function(isWater) {
const RN = isWater.length, CN = isWater[0].length;
const output = [...Array(RN)].map(() => Array(CN).fill(-1));
const dir = [[1, 0], [-1, 0], [0, 1], [0, -1]]
let queue = []
for(let r = 0; r < RN; r++) {
for(let c = 0; c < CN; c++) {
if(isWater[r][c]) {
queue.push([r, c]);
output[r][c] = 0;
}
}
}
while(queue.length) {
const next = []
for(let [r, c] of queue) {
for(let [dr, dc] of dir) {
dr += r;
dc += c;
if(dr < 0 || dc < 0 || dr >= RN || dc >= CN || output[dr][dc] !== -1) continue;
output[dr][dc] = output[r][c] + 1;
next.push([dr, dc]);
}
}
queue = next;
}
return output;
};