-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
48 lines (36 loc) · 1.26 KB
/
Copy pathSolution.java
File metadata and controls
48 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
44
45
46
47
48
import java.util.LinkedList;
import java.util.Queue;
class Solution {
private static final int[][] DIRECTIONS = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
public int[][] updateMatrix(int[][] mat) {
int n = mat.length, m = mat[0].length;
int[][] result = new int[n][m];
Queue<int[]> queue = new LinkedList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mat[i][j] == 0) {
queue.add(new int[]{i, j});
} else {
result[i][j] = Integer.MAX_VALUE;
}
}
}
int steps = 0;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
int[] curr = queue.poll();
int x = curr[0], y = curr[1];
for (int[] dir : DIRECTIONS) {
int a = x + dir[0], b = y + dir[1];
if (a >= 0 && a < n && b >= 0 && b < m && result[a][b] == Integer.MAX_VALUE) {
result[a][b] = 1 + steps;
queue.add(new int[]{a, b});
}
}
}
steps++;
}
return result;
}
}