-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path994.rotting-oranges.java
52 lines (46 loc) · 1.46 KB
/
994.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* @lc app=leetcode id=994 lang=java
*
* [994] Rotting Oranges
*/
// @lc code=start
class Solution {
public int orangesRotting(int[][] grid) {
int m = grid.length;
if (m == 0) return 0;
int n = grid[0].length;
Queue<int[]> queue = new LinkedList<>();
int freshOrange = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 2) {
queue.add(new int[]{i, j});
}
if (grid[i][j] == 1) {
freshOrange++;
}
}
}
int level = 0;
int[][] directions = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
while (!queue.isEmpty() && freshOrange > 0) {
int size = queue.size();
for (int i = 0; i < size; i++) {
int[] point = queue.poll();
for (int[] dir : directions) {
int newRow = point[0] + dir[0];
int newCol = point[1] + dir[1];
if (newRow < 0 || newRow >= m || newCol < 0 || newCol >= n || grid[newRow][newCol] != 1) {
continue;
}
grid[newRow][newCol] = 2;
queue.add(new int[]{newRow, newCol});
freshOrange--;
}
}
level++;
}
return freshOrange == 0 ? level : -1;
}
}
// @lc code=end