Skip to content

Commit 3a3b293

Browse files
authored
Merge pull request kelvins#324 from Hardvan/rotten-oranges
Add Rotten Oranges Graph Problem using BFS
2 parents 23c63ad + aa0ffe9 commit 3a3b293

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3840,6 +3840,64 @@ In order to achieve greater coverage and encourage more people to contribute to
38403840
</a>
38413841
</td>
38423842
</tr>
3843+
<tr>
3844+
<td>Rotten Oranges</td>
3845+
<td> <!-- C -->
3846+
<a href="./CONTRIBUTING.md">
3847+
<img align="center" height="25" src="./logos/github.svg" />
3848+
</a>
3849+
</td>
3850+
<td> <!-- C++ -->
3851+
<a href="./src/cpp/RottenOranges.cpp">
3852+
<img align="center" height="25" src="./logos/cplusplus.svg" />
3853+
</a>
3854+
</td>
3855+
<td> <!-- Java -->
3856+
<a href="./CONTRIBUTING.md">
3857+
<img align="center" height="25" src="./logos/github.svg" />
3858+
</a>
3859+
</td>
3860+
<td> <!-- Python -->
3861+
<a href="./CONTRIBUTING.md">
3862+
<img align="center" height="25" src="./logos/github.svg" />
3863+
</a>
3864+
</td>
3865+
<td> <!-- Go -->
3866+
<a href="./CONTRIBUTING.md">
3867+
<img align="center" height="25" src="./logos/github.svg" />
3868+
</a>
3869+
</td>
3870+
<td> <!-- Ruby -->
3871+
<a href="./CONTRIBUTING.md">
3872+
<img align="center" height="25" src="./logos/github.svg" />
3873+
</a>
3874+
</td>
3875+
<td> <!-- JavaScript -->
3876+
<a href="./CONTRIBUTING.md">
3877+
<img align="center" height="25" src="./logos/github.svg" />
3878+
</a>
3879+
</td>
3880+
<td> <!-- Swift -->
3881+
<a href="./CONTRIBUTING.md">
3882+
<img align="center" height="25" src="./logos/github.svg" />
3883+
</a>
3884+
</td>
3885+
<td> <!-- Rust -->
3886+
<a href="./CONTRIBUTING.md">
3887+
<img align="center" height="25" src="./logos/github.svg" />
3888+
</a>
3889+
</td>
3890+
<td> <!-- Scala -->
3891+
<a href="./CONTRIBUTING.md">
3892+
<img align="center" height="25" src="./logos/github.svg" />
3893+
</a>
3894+
</td>
3895+
<td> <!-- Kotlin -->
3896+
<a href="./CONTRIBUTING.md">
3897+
<img align="center" height="25" src="./logos/github.svg" />
3898+
</a>
3899+
</td>
3900+
</tr>
38433901
<tr>
38443902
<td>Isogram</td>
38453903
<td> <!-- C -->

src/cpp/RottenOranges.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/*
6+
Problem Statement:
7+
Given an m x n grid, where each cell has the following values:
8+
2 – Rotten orange
9+
1 – Fresh orange
10+
0 – Empty cell
11+
12+
Every minute, if a Fresh orange is adjacent to a Rotten Orange in 4-direction
13+
(upward, downwards, right, and left) it becomes Rotten.
14+
15+
Return the minimum number of minutes required such that
16+
none of the cells has a Fresh Orange.
17+
If it's not possible, return -1.
18+
*/
19+
20+
// Approach: BFS
21+
22+
bool isValid(int nx, int ny, int m, int n)
23+
{
24+
return (0 <= nx && nx < m) &&
25+
(0 <= ny && ny < n);
26+
}
27+
28+
// Time: O(mn) x 4
29+
// Space: O(mn)
30+
int orangesRotting(vector<vector<int>> &grid)
31+
{
32+
int m = grid.size();
33+
int n = grid[0].size();
34+
35+
int total = 0; // fresh + rotten oranges
36+
int count = 0; // rotten oranges
37+
int mins = 0; // minutes elapsed
38+
39+
queue<pair<int, int>> rotten; // {i, j}: position of rotten orange
40+
41+
// Count the fresh & rotten oranges, push rotten oranges into queue
42+
for (int i = 0; i < m; i++)
43+
{
44+
for (int j = 0; j < n; j++)
45+
{
46+
if (grid[i][j] != 0) // Rotten or Fresh orange
47+
total++;
48+
if (grid[i][j] == 2) // Rotten
49+
rotten.push({i, j}); // Push position of rotten orange
50+
}
51+
}
52+
53+
int dx[] = {0, 0, -1, 1}; // 4-directions (x-axis)
54+
int dy[] = {-1, 1, 0, 0}; // 4-directions (y-axis)
55+
56+
while (!rotten.empty())
57+
{
58+
int size = rotten.size(); // rotten oranges in current minute
59+
count += size; // add to total rotten oranges
60+
61+
while (size--) // Each rotten orange in current minute
62+
{
63+
// Pop the front rotten orange
64+
int x = rotten.front().first;
65+
int y = rotten.front().second;
66+
rotten.pop();
67+
68+
// Check for fresh oranges in 4-directions
69+
for (int i = 0; i < 4; i++)
70+
{
71+
// New coordinates
72+
int nx = x + dx[i];
73+
int ny = y + dy[i];
74+
75+
// Valid, fresh orange
76+
if (isValid(nx, ny, m, n) && grid[nx][ny] == 1)
77+
{
78+
grid[nx][ny] = 2; // make it rotten
79+
rotten.push({nx, ny}); // push it into queue
80+
}
81+
}
82+
}
83+
84+
if (!rotten.empty()) // if there are more rotten oranges
85+
mins++;
86+
}
87+
88+
if (total != count) // fresh oranges left
89+
return -1;
90+
91+
return mins;
92+
}
93+
94+
int main()
95+
{
96+
vector<vector<int>> grid = {{2, 1, 1},
97+
{1, 1, 0},
98+
{0, 1, 1}};
99+
100+
cout << orangesRotting(grid) << endl; // 4
101+
102+
return 0;
103+
}

0 commit comments

Comments
 (0)