-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1162-AsFarLandAsPossible.java
More file actions
43 lines (36 loc) · 1.26 KB
/
Copy path1162-AsFarLandAsPossible.java
File metadata and controls
43 lines (36 loc) · 1.26 KB
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
class Solution {
public int maxDistance(int[][] grid) {
int n = grid.length;
int[][] directions = { { -1, 0 }, { 1, 0 }, { 0, 1 }, { 0, -1 } };
Queue<int[]> queue = new LinkedList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
queue.add(new int[] { i, j });
}
}
}
if (queue.size() == n * n || queue.size() == 0)
return -1;
int level = 0;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
int[] curr = queue.poll();
int row = curr[0];
int col = curr[1];
for (int[] d : directions) {
int nextRow = row + d[0];
int nextCol = col + d[1];
if (nextRow < 0 || nextCol < 0 || nextRow >= n || nextCol >= n ||
grid[nextRow][nextCol] != 0)
continue;
grid[nextRow][nextCol] = 1;
queue.add(new int[] { nextRow, nextCol });
}
}
level += 1;
}
return level - 1;
}
}