Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Rotting Apples Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Rotting Apples

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

You are given an n * m grid where each position can contain one of the three values:

Cell ValueRepresents
0Empty Cell
1Fresh Apple
2Rotten Apple

Every day, all fresh apples which are adjacent to any rotten apple become rotten.

Two cells are adjacent if they have a common edge, i.e., each cell can have upto four adjacent cells.

Find the minimum number of days required for all the apples to be rotten. If this is not possible return -1.

Approach

The way to approach this problem is to use “Multisource BFS on a Grid”.  We can observe that fresh apples, adjacent to the rotten apples are rotten on day 1, then those adjacent to these apples are rotten on day 2, and so on. This is similar to a level order traversal on a graph, where the rotten apples are the initial root nodes. We can push all the initial rotten apple nodes into our queue and perform BFS on a grid, and calculate the total time taken. If all the fresh nodes cannot be rotten after the end of the BFS call, we return -1.

Analysis

  • Time Complexity: O(n * m)
  • Space Complexity: O(n * m)

Implementation

C++
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
bool isSafe(vector<vector<int> > &grid, int x, int y){
    int r = grid.size();
    int c = grid[0].size();
    return (x >= 0 && x < r && y >= 0 && y < c && grid[x][y] == 1);
}
int getDaysToRot(vector<vector<int>> &grid) {
    int r = grid.size();
    int c = grid[0].size();
    queue<pair<int, int>> q;
    for(int i = 0; i < r; i++){
        for(int j = 0; j < c; j++){
            if(grid[i][j] == 2) {
				q.push({i, j});
			}
        }
    }
    int days = -1;
    while(!q.empty()) {
        int qs = q.size();
            while(qs--) {
            pair<int, int> p = q.front();
            int x = p.first;
            int y = p.second;
            q.pop();
            for(int i = 0; i < 4; i++){
                if(isSafe(grid, x + dx[i], y + dy[i])){
                    q.push({x + dx[i], y + dy[i]});
                    grid[x + dx[i]][y + dy[i]] = 2;
                }
            }
        }
        days += 1;
    }
    for(int i = 0; i < r; i++){
        for(int j = 0; j < c; j++){
            if(grid[i][j] == 1){
				return -1;
			}
        }
    }
    return days;
}
Java
class Solution {
	int getDaysToRot(int[][] grid) {
	    int[][] dir = {{0,-1}, {-1,0}, {0,1}, {1,0}};
		int r = grid.length;
		int c = grid[0].length;
		int time = 0, countOfOnes = 0;
		Queue<int[]> q = new LinkedList<>();
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++) { 
				if(grid[i][j] == 2) {
					q.add(new int[] {i,j});
				}
				if(grid[i][j] == 1) { 
					countOfOnes++;
				}
			}
		}
		if(countOfOnes == 0) { 
			return 0;
		}
		while(!q.isEmpty()) {
			int size = q.size();
			while(size-- > 0) {
				int[] temp = q.remove();
				int i = temp[0], j = temp[1];
				for(int l = 0; l < 4; l++){
					int nr = i + dir[l][0], nc = j + dir[l][1];
					if(nr < 0 || nc < 0 || nr == r || nc == c) {  
						continue;
					}
					if(grid[nr][nc] == 1) {
						countOfOnes -= 1;
						q.add(new int[] {nr,nc});
						grid[nr][nc] = 2;
					}
				}
			}
			if(q.size() > 0) {
				time += 1;
			}
		}
		if(countOfOnes == 0) {
			return time;
		}
		else {
			return -1;
		}
	}
}
Related Content
Implement Queue using Array
Implement Queue using Linked List
Implement Queue using Stacks
Implement Stack using Queues
Sliding Window Maximum
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet