-
Notifications
You must be signed in to change notification settings - Fork 277
/
RottingOrganges.java
83 lines (65 loc) · 2.03 KB
/
RottingOrganges.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class Solution {
private class Point {
private int x;
private int y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
}
// BFS traversal Graph solution
public int orangesRotting(int[][] grid) {
int n = grid.length;
int m = grid[0].length;
if(n==0 || m==0){
return -1;
}
Queue<Point> qu = new LinkedList<>();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(grid[i][j] == 2){
qu.offer(new Point(i, j));
}
}
}
int level =0;
while(!qu.isEmpty()) {
int size = qu.size();
while(size-->0){
Point head = qu.poll();
int i = head.x;
int j = head.y;
if(i<0 || j<0 || i>=n || j>=m || grid[i][j] <=0){
continue;
}
grid[i][j] = -1;
if(i>=1 && grid[i-1][j]==1){
grid[i-1][j] = 3; // rotton organes will be treated with no 2, 3
qu.offer(new Point(i-1,j));
}
if(i<n-1 && grid[i+1][j]==1){
grid[i+1][j] = 3; // rotton oranges will be treated with no 2, 3
qu.offer(new Point(i+1,j));
}
if(j>=1 && grid[i][j-1]==1){
grid[i][j-1] = 3; // rotton oranges will be treated with no 2, 3
qu.offer(new Point(i,j-1));
}
if(j<m-1 && grid[i][j+1]==1){
grid[i][j+1] = 3; // rotton oranges will be treated with no 2, 3
qu.offer(new Point(i,j+1));
}
}
level++;
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(grid[i][j]==1){
return -1;
}
}
}
// [[0]]
return level>0 ? level-1 : 0;
}
}