-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1162-as-far-from-land-as-possible.js
56 lines (50 loc) · 1.61 KB
/
1162-as-far-from-land-as-possible.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
/**
* 1162. As Far from Land as Possible
* https://leetcode.com/problems/as-far-from-land-as-possible/
* Difficulty: Medium
*
* Given an n x n grid containing only values 0 and 1, where 0 represents water and 1 represents
* land, find a water cell such that its distance to the nearest land cell is maximized, and return
* the distance. If no land or water exists in the grid, return -1.
*
* The distance used in this problem is the Manhattan distance: the distance between two cells
* (x0, y0) and (x1, y1) is |x0 - x1| + |y0 - y1|.
*/
/**
* @param {number[][]} grid
* @return {number}
*/
var maxDistance = function(grid) {
const size = grid.length;
const queue = [];
let waterCount = 0;
for (let row = 0; row < size; row++) {
for (let col = 0; col < size; col++) {
if (grid[row][col] === 1) {
queue.push([row, col]);
} else {
waterCount++;
}
}
}
if (waterCount === 0 || queue.length === 0) return -1;
const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];
let distance = -1;
while (queue.length > 0) {
distance++;
const levelSize = queue.length;
for (let i = 0; i < levelSize; i++) {
const [currentRow, currentCol] = queue.shift();
for (const [deltaRow, deltaCol] of directions) {
const newRow = currentRow + deltaRow;
const newCol = currentCol + deltaCol;
if (newRow >= 0 && newRow < size && newCol >= 0 && newCol < size
&& grid[newRow][newCol] === 0) {
grid[newRow][newCol] = 1;
queue.push([newRow, newCol]);
}
}
}
}
return distance === 0 ? -1 : distance;
};