8000 Add c++ solution at algorithms/cpp/rottingOranges/rottingOranges.cpp · royeLin/leetcode_cpp@1d7638c · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d7638c

Browse files
committed
Add c++ solution at algorithms/cpp/rottingOranges/rottingOranges.cpp
1 parent cf8a73d commit 1d7638c

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Source : https://leetcode.com/problems/rotting-oranges/
2+
// Author : David Lin
3+
// Date : 2023-05-18
4+
5+
/**********************************************************************************
6+
*
7+
You are given an m x n grid where each cell can have one of three values:
8+
9+
0 representing an empty cell,
10+
1 representing a fresh orange, or
11+
2 representing a rotten orange.
12+
Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.
13+
14+
Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.
15+
*
16+
* Example 1:
17+
* 211
18+
* 110
19+
* 011
20+
* Answer: 4
21+
*
22+
* Example 2:
23+
* 211
24+
* 011
25+
* 101
26+
* Answer: -1
27+
*
28+
* Example 3:
29+
* 02
30+
* Answer: 0
31+
*
32+
*
33+
*
34+
**********************************************************************************/
35+
36+
#include <iostream>
37+
#include <vector>
38+
#include <queue>
39+
#include <stack>
40+
41+
using namespace std;
42+
43+
44+
int orangesRotting(vector<vector<int> >& grid) {
45+
int minutes{};
46+
int num_fresh{};
47+
queue<pair<int, int>> q;
48+
int row_size = grid.size();
49+
int col_size = grid[0].size();
50+
for(int row{}; row < row_size; row++){
51+
for(int col{}; col< col_size; col++){
52+
switch (grid[row][col])
53+
{
54+
case 2:
55+
q.push({row, col});
56+
break;
57+
case 1:
58+
num_fresh++;
59+
break;
60+
default:
61+
break;
62+
}
63+
}
64+
}
65+
66+
vector<pair<int, int>> directions = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
67+
int q_size = q.size();
68+
if (q_size == 0){
69+
if (num_fresh == 0)return 0;
70+
return -1;
71+
}
72+
73+
while (!q.empty())
74+
{
75+
if(q_size == 0 && q.size() > 0){
76+
minutes++;
77+
q_size = q.size();
78+
}
79+
int row = q.front().first;
80+
int col = q.front().second;
81+
q.pop();
82+
83+
for(auto& direction: directions){
84+
int new_row = row + direction.first;
85+
int new_col = col + direction.second;
86+
if(new_row < row_size && new_row >= 0 && new_col < col_size && new_col >= 0){
87+
if(grid[new_row][new_col] == 1){
88+
grid[new_row][new_col] = 2;
89+
q.push({new_row, new_col});
90+
num_fresh--;
91+
}
92+
}
93+
}
94+
q_size--;
95+
}
96+
if(num_fresh > 0)return -1;
97+
return minutes;
98+
}
99+
100+
101+
int main(void)
102+
{
103+
vector< vector<int> > grid;
104+
grid.push_back( vector<int>(1, 0) );
105+
cout << orangesRotting(grid) << endl;
106+
grid.clear();
107+
108+
grid = {{2,1,1},{1,1,0},{0,1,1}};
109+
cout << orangesRotting(grid) << endl;
110+
grid.clear();
111+
112+
grid = {{2,1,1},{0,1,1},{1,0,1}};
113+
cout << orangesRotting(grid) << endl;
114+
grid.clear();
115+
116+
grid = {{0,2}};
117+
cout << orangesRotting(grid) << endl;
118+
return 0;
119+
}

0 commit comments

Comments
 (0)
0