-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
42 lines (34 loc) · 1.28 KB
/
solution.java
File metadata and controls
42 lines (34 loc) · 1.28 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
class Solution {
public int orangesRotting(int[][] grid) {
int R = grid.length, C = grid[0].length;
int time = 0, freshOrng = 0;
LinkedList<int[]> queue = new LinkedList<>();
for(int i = 0; i < R; i++){
for(int j = 0; j < C; j++){
if(grid[i][j] == 2){
queue.add(new int[]{i, j});
} else if(grid[i][j] == 1){
freshOrng++;
}
}
}
int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
while(freshOrng > 0 && !queue.isEmpty()){
int size = queue.size();
for(int i = 0; i < size; i++){
int[] pos = queue.poll();
for(int[] dir : dirs){
int row = pos[0] + dir[0];
int col = pos[1] + dir[1];
if(row >= 0 && col >= 0 && row < R && col < C && grid[row][col] == 1){
queue.add(new int[]{row, col});
grid[row][col] = 2;
freshOrng--;
}
}
}
time++;
}
return freshOrng > 0 ? -1 : time;
}
}