-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0994-rotting-oranges.java
36 lines (33 loc) · 1.18 KB
/
0994-rotting-oranges.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
class Solution {
public int orangesRotting(int[][] grid) {
int m = grid.length, n = grid[0].length;
Queue<int[]> queue = new LinkedList<>();
int fresh = 0;
for (int i = 0; i < m; i += 1) {
for (int j = 0; j < n; j += 1) {
if (grid[i][j] == 2) queue.offer(new int[] { i, j }); else if (
grid[i][j] == 1
) fresh += 1;
}
}
int count = 0;
int[][] dirs = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
while (!queue.isEmpty() && fresh != 0) {
count += 1;
int sz = queue.size();
for (int i = 0; i < sz; i += 1) {
int[] rotten = queue.poll();
int r = rotten[0], c = rotten[1];
for (int[] dir : dirs) {
int x = r + dir[0], y = c + dir[1];
if (0 <= x && x < m && 0 <= y && y < n && grid[x][y] == 1) {
grid[x][y] = 2;
queue.offer(new int[] { x, y });
fresh -= 1;
}
}
}
}
return fresh == 0 ? count : -1;
}
}