-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orangesRotting.cpp
41 lines (32 loc) · 1.28 KB
/
orangesRotting.cpp
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
class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
queue<pair<int, int>> q;
int freshOranges = 0;
int time = 0;
vector<vector<int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
for(int i = 0; i < grid.size(); ++i) {
for(int j = 0; j < grid[i].size(); ++j) {
if(grid[i][j] == 2) q.push(make_pair(i, j));
if(grid[i][j] == 1) freshOranges++;
}
}
while(!q.empty() && freshOranges > 0) {
time++;
int size = q.size();
while(size-- > 0) {
pair<int, int> coords = q.front();
q.pop();
for(vector<int> d: directions) {
int x = coords.first + d[0];
int y = coords.second + d[1];
if(x < 0 || x >= grid.size() || y < 0 || y >= grid[x].size() || grid[x][y] == 0 || grid[x][y] == 2) continue;
grid[x][y] = 2;
freshOranges--;
q.push(make_pair(x, y));
}
}
}
return freshOranges == 0 ? time : -1;
}
};