-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay 177.java
More file actions
52 lines (41 loc) · 1.42 KB
/
Copy pathDay 177.java
File metadata and controls
52 lines (41 loc) · 1.42 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
49
50
51
52
import java.util.*;
class Solution {
public int orangesRot(int[][] mat) {
int n = mat.length;
int m = mat[0].length;
Queue<int[]> q = new LinkedList<>();
int fresh = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mat[i][j] == 2) {
q.offer(new int[]{i, j});
} else if (mat[i][j] == 1) {
fresh++;
}
}
}
if (fresh == 0) return 0;
int time = 0;
int[][] dirs = {{1,0},{-1,0},{0,1},{0,-1}};
while (!q.isEmpty()) {
int size = q.size();
boolean rotted = false;
for (int i = 0; i < size; i++) {
int[] curr = q.poll();
int r = curr[0], c = curr[1];
for (int[] d : dirs) {
int nr = r + d[0];
int nc = c + d[1];
if (nr >= 0 && nc >= 0 && nr < n && nc < m && mat[nr][nc] == 1) {
mat[nr][nc] = 2;
q.offer(new int[]{nr, nc});
fresh--;
rotted = true;
}
}
}
if (rotted) time++;
}
return fresh == 0 ? time : -1;
}
}