forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_994.java
49 lines (46 loc) · 1.69 KB
/
_994.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
package com.fishercoder.solutions;
import java.util.LinkedList;
import java.util.Queue;
public class _994 {
public static class Solution1 {
int[] directions = new int[]{0, 1, 0, -1, 0};
public int orangesRotting(int[][] grid) {
Queue<int[]> rottens = new LinkedList<>();
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 2) {
rottens.add(new int[]{i, j});
}
}
}
int times = 0;
while (!rottens.isEmpty()) {
int size = rottens.size();
boolean counted = false;
for (int k = 0; k < size; k++) {
int[] rotten = rottens.poll();
for (int i = 0; i < 4; i++) {
int x = rotten[0] + directions[i];
int y = rotten[1] + directions[i + 1];
if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length && grid[x][y] == 1) {
grid[x][y] = 2;
if (!counted) {
times++;
}
counted = true;
rottens.add(new int[]{x, y});
}
}
}
}
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1) {
return -1;
}
}
}
return times;
}
}
}