-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrottingOranges.cpp
More file actions
39 lines (38 loc) · 1.19 KB
/
rottingOranges.cpp
File metadata and controls
39 lines (38 loc) · 1.19 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
// Source: https://leetcode.com/problems/rotting-oranges/
// Author: Miao Zhang
// Date: 2021-03-31
class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
queue<pair<int, int>> q;
int fresh = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) fresh++;
else if (grid[i][j] == 2) q.emplace(i, j);
}
}
vector<vector<int>> dirs{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int days = 0;
while (!q.empty() && fresh) {
int size = q.size();
while (size--) {
int i = q.front().first;
int j = q.front().second;
q.pop();
for (auto& d: dirs) {
int x = i + d[0];
int y = j + d[1];
if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] != 1) continue;
fresh--;
grid[x][y] = 2;
q.emplace(x, y);
}
}
days++;
}
return fresh ? -1 : days;
}
};