-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotten oranges
More file actions
60 lines (52 loc) · 1.53 KB
/
Copy pathRotten oranges
File metadata and controls
60 lines (52 loc) · 1.53 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
53
54
55
56
57
58
59
60
Input: mat[][] =
[[0, 1, 2],
[0, 1, 2],
[2, 1, 1]]
Output: 1
Explanation: Oranges at positions (0,2), (1,2), (2,0) will rot oranges at (0,1), (1,1), (2,2) and (2,1) in unit time.
class Solution {
public int orangesRotting(int[][] grid) {
int n = grid.length;
int m = grid[0].length;
int fresh=0;
Queue<int[]> q = new LinkedList<>();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(grid[i][j]==2){
q.add(new int[]{i,j});
}
else if(grid[i][j]==1){
fresh++;
}
}
}
if(fresh==0){
return 0;
}
int minutes=0;
int dir[][] = {{-1,0},{1,0},{0,-1},{0,1}};
while(!q.isEmpty()){
int size = q.size();
boolean hasrotten = false;
for(int i=0;i<size;i++){
int curr[] = q.remove();
int x = curr[0];
int y = curr[1];
for(int dirr[] : dir){
int nx = x+dirr[0];
int ny = y+dirr[1];
if(nx>=0 && nx<n && ny>=0 && ny<m && grid[nx][ny]==1){
grid[nx][ny]=2;
q.add(new int[]{nx,ny});
fresh--;
hasrotten = true;
}
}
}
if(hasrotten){
minutes++;
}
}
return fresh==0 ? minutes : -1;
}
}