-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsFarFromLandAsPossible.java
68 lines (55 loc) · 1.95 KB
/
AsFarFromLandAsPossible.java
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
/* https://leetcode.com/problems/as-far-from-land-as-possible/description/ */
/*
1162. As Far from Land as Possible
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|.
Example 1:
Input: grid = [[1,0,1],[0,0,0],[1,0,1]]
Output: 2
Explanation: The cell (1, 1) is as far as possible from all the land with distance 2.
Example 2:
Input: grid = [[1,0,0],[0,0,0],[0,0,0]]
Output: 4
Explanation: The cell (2, 2) is as far as possible from all the land with distance 4.
Constraints:
n == grid.length
n == grid[i].length
1 <= n <= 100
grid[i][j] is 0 or 1
*/
class AsFarFromLandAsPossible {
public int maxDistance(int[][] grid) {
final int m = grid.length;
final int n = grid[0].length;
final int[] dirs = {0, 1, 0, -1, 0};
Queue<Pair<Integer, Integer>> q = new ArrayDeque<>();
int water = 0;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (grid[i][j] == 0)
++water;
else
q.offer(new Pair<>(i, j));
if (water == 0 || water == m * n)
return -1;
int ans = 0;
for (int d = 0; !q.isEmpty(); ++d)
for (int sz = q.size(); sz > 0; --sz) {
Pair<Integer, Integer> pair = q.poll();
final int i = pair.getKey();
final int j = pair.getValue();
ans = d;
for (int k = 0; k < 4; ++k) {
final int x = i + dirs[k];
final int y = j + dirs[k + 1];
if (x < 0 || x == m || y < 0 || y == n)
continue;
if (grid[x][y] > 0)
continue;
q.offer(new Pair<>(x, y));
grid[x][y] = 2; // Mark as visited.
}
}
return ans;
}
}