994. Rotting Oranges

problem

solution

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
class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
queue<vector<int>> q;
// 紀錄新鮮的橘子個數 (grid[i][j] = 1)
int fresh_cnt = 0;
int n= grid.size(), m = grid[0].size();
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
if(grid[i][j] == 2) q.push({i,j});
else if(grid[i][j] == 1) fresh_cnt++;
}
}

vector<vector<int>> dirs = {{-1,0}, {0,-1},{1,0},{0,1}};
int step = 0;
while(!q.empty() && fresh_cnt){
int size =q.size();
for(int i=0;i<size ; ++i){
vector<int> p = q.front();
q.pop();
for(auto d:dirs){
int x= p[0]+d[0], y = p[1] + d[1];
if(x< 0 || y<0 || x>n-1 || y>m-1 || grid[x][y] !=1) continue;
grid[x][y] = 2;
q.push({x,y});
// 拜訪過所以腐爛,所以減一

fresh_cnt--;
}
}
step++;
}
// 如果還有新鮮橘子
if(fresh_cnt>0) return -1;
return step;

}
};

analysis

  • time complexity O(n*m)
  • space complexity O(n*m)