Skip to main content

Rotting Oranges

Problem

You are given an m x n grid where each cell can have one of three values:

  • 0 representing an empty cell,
  • 1 representing a fresh orange, or
  • 2 representing a rotten orange.

Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.

Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.

 

Example 1:


Input: grid = [[2,1,1],[1,1,0],[0,1,1]] 
Output: 4

Solution Approach

Expected Time Complexity: O(nn)O(n^n)

Click - to see solution code
class Solution {
deque<pair<int, int>> dq;
vector<vector<int>> ans, vis, vis1;
int n, m;
vector<vector<int>> g;

public:
void dfs(int i, int j, int time) {
if (vis[i][j] == 1 && ans[i][j] < time) return;
vis[i][j] = 1;
ans[i][j] = min(ans[i][j], time);

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
for (int k = 0; k < 4; k++) {
int x = i + dx[k];
int y = j + dy[k];
if ((!(x < 0 || x >= n || y < 0 || y >= m)) && g[x][y] == 1) {
dfs(x, y, time + 1);
}
}
}

int orangesRotting(vector<vector<int>>& grid) {
g = grid;
n = grid.size();
m = grid[0].size();
ans.resize(n, vector<int>(m, INT_MAX));
vis = ans;
vis1 = vis;

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 2) {
g = grid;
vis = vis1;
dfs(i, j, 0);
}
}
}

int res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] != 0) {
res = max(res, ans[i][j]);
if (res == INT_MAX) return -1;
}
}
}

return res;
}
};