Skip to content

Commit add5514

Browse files
committed
0994 added.
1 parent 2b6d1a4 commit add5514

File tree

4 files changed

+212
-0
lines changed

4 files changed

+212
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
project(B)
3+
4+
set(CMAKE_CXX_STANDARD 11)
5+
6+
add_executable(B main2.cpp)
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/// Source : https://leetcode.com/problems/rotting-oranges/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-02-16
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <cassert>
8+
#include <unordered_set>
9+
10+
using namespace std;
11+
12+
13+
/// Simulation
14+
/// Time Complexity: O(n)
15+
/// Space Complexity: O(n)
16+
class Solution {
17+
18+
private:
19+
const int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
20+
int m, n;
21+
22+
public:
23+
int orangesRotting(vector<vector<int>>& grid) {
24+
25+
assert(grid.size() && grid[0].size());
26+
m = grid.size(), n = grid[0].size();
27+
28+
vector<int> rotten;
29+
int fresh = 0;
30+
for(int i = 0; i < m; i ++)
31+
for(int j = 0; j < n; j ++)
32+
if(grid[i][j] == 1)
33+
fresh ++;
34+
else if(grid[i][j] == 2)
35+
rotten.push_back(i * n + j);
36+
37+
if(!rotten.size())
38+
return fresh ? -1 : 0;
39+
40+
unordered_set<int> used;
41+
for(int e: rotten) used.insert(e);
42+
43+
int res = 0;
44+
while(rotten.size()){
45+
46+
// cout << "rotten : ";
47+
// for(int e: rotten) cout << e << " ";
48+
// cout << endl;
49+
50+
vector<int> newrotten;
51+
for(int e: rotten){
52+
int x = e / n, y = e % n;
53+
for(int i = 0; i < 4; i ++){
54+
int nextx = x + d[i][0], nexty = y + d[i][1];
55+
if(nextx >= 0 && nextx < m && nexty >= 0 && nexty < n && grid[nextx][nexty] == 1){
56+
grid[nextx][nexty] = 2;
57+
newrotten.push_back(nextx * n + nexty);
58+
used.insert(nextx * n + nexty);
59+
}
60+
}
61+
}
62+
63+
if(newrotten.size()) res ++;
64+
rotten = newrotten;
65+
}
66+
67+
if(!allRotten(grid)) return -1;
68+
return res;
69+
}
70+
71+
private:
72+
bool allRotten(const vector<vector<int>>& grid){
73+
for(int i = 0; i < m; i ++)
74+
for(int j = 0; j < n; j ++)
75+
if(grid[i][j] == 1)
76+
return false;
77+
return true;
78+
}
79+
};
80+
81+
82+
int main() {
83+
84+
vector<vector<int>> grid1 = {
85+
{2,1,1},
86+
{1,1,0},
87+
{0,1,1}
88+
};
89+
cout << Solution().orangesRotting(grid1) << endl;
90+
// 4
91+
92+
vector<vector<int>> grid2 = {
93+
{0, 0},
94+
{0, 0}
95+
};
96+
cout << Solution().orangesRotting(grid2) << endl;
97+
// 0
98+
99+
vector<vector<int>> grid3 = {
100+
{1}
101+
};
102+
cout << Solution().orangesRotting(grid3) << endl;
103+
// -1
104+
105+
return 0;
106+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/// Source : https://leetcode.com/problems/rotting-oranges/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-02-16
4+
5+
#include <iostream>
6+
#include <queue>
7+
#include <cassert>
8+
#include <unordered_set>
9+
10+
using namespace std;
11+
12+
13+
/// BFS
14+
/// Time Complexity: O(n)
15+
/// Space Complexity: O(n)
16+
class Solution {
17+
18+
private:
19+
const int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
20+
int m, n;
21+
22+
public:
23+
int orangesRotting(vector<vector<int>>& grid) {
24+
25+
assert(grid.size() && grid[0].size());
26+
m = grid.size(), n = grid[0].size();
27+
28+
queue<pair<int, int>> q;
29+
unordered_set<int> used;
30+
int fresh = 0;
31+
for(int i = 0; i < m; i ++)
32+
for(int j = 0; j < n; j ++)
33+
if(grid[i][j] == 2){
34+
int pos = i * n + j;
35+
q.push(make_pair(pos, 0));
36+
used.insert(pos);
37+
}
38+
39+
int res = 0;
40+
while(!q.empty()){
41+
42+
int cur = q.front().first;
43+
int step = q.front().second;
44+
q.pop();
45+
46+
res = max(res, step);
47+
48+
int x = cur / n, y = cur % n;
49+
for(int i = 0; i < 4; i ++){
50+
int nextx = x + d[i][0], nexty = y + d[i][1];
51+
if(nextx >= 0 && nextx < m && nexty >= 0 && nexty < n && grid[nextx][nexty] == 1){
52+
int nextpos = nextx * n + nexty;
53+
grid[nextx][nexty] = 2;
54+
q.push(make_pair(nextpos, step + 1));
55+
used.insert(nextpos);
56+
}
57+
}
58+
}
59+
60+
if(!allRotten(grid)) return -1;
61+
return res;
62+
}
63+
64+
private:
65+
bool allRotten(const vector<vector<int>>& grid){
66+
for(int i = 0; i < m; i ++)
67+
for(int j = 0; j < n; j ++)
68+
if(grid[i][j] == 1)
69+
return false;
70+
return true;
71+
}
72+
};
73+
74+
75+
int main() {
76+
77+
vector<vector<int>> grid1 = {
78+
{2,1,1},
79+
{1,1,0},
80+
{0,1,1}
81+
};
82+
cout << Solution().orangesRotting(grid1) << endl;
83+
// 4
84+
85+
vector<vector<int>> grid2 = {
86+
{0, 0},
87+
{0, 0}
88+
};
89+
cout << Solution().orangesRotting(grid2) << endl;
90+
// 0
91+
92+
vector<vector<int>> grid3 = {
93+
{1}
94+
};
95+
cout << Solution().orangesRotting(grid3) << endl;
96+
// -1
97+
98+
return 0;
99+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,4 +658,5 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
658658
| 991 | [Broken Calculator](https://leetcode.com/problems/broken-calculator/) | [solution](https://leetcode.com/problems/broken-calculator/solution/) | [C++](0991-Broken-Calculator/cpp-0991/) | | |
659659
| 992 | [Subarrays with K Different Integers](https://leetcode.com/problems/subarrays-with-k-different-integers/) | [solution](https://leetcode.com/problems/subarrays-with-k-different-integers/solution/) | [C++](0992-Subarrays-with-K-Different-Integers/cpp-0992/) | | |
660660
| 993 | [Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/) | [solution](https://leetcode.com/problems/cousins-in-binary-tree/solution/) | [C++](0993-Cousins-in-Binary-Tree/cpp-0993/) | | |
661+
| 994 | [Rotting Oranges](https://leetcode.com/problems/rotting-oranges/) | [solution](https://leetcode.com/problems/rotting-oranges/solution/) | [C++](0994-Rotting-Oranges/cpp-0994/) | | |
661662
| | | | | | |

0 commit comments

Comments
 (0)