# Rotting Oranges

Category

Graphs - BFS

Checkbox

Checkbox

Difficulty

Medium

Index

48

Key Ideas

The key idea to solve this problem is to use Breadth-First Search (BFS) to simulate the rotting process of oranges in a matrix.

Problem Number

994

Problem Summary

The "Rotting Oranges" problem is from LeetCode-75 curated list. It involves a grid of oranges, where rotten oranges infect adjacent fresh oranges in each minute. The goal is to determine the minimum number of minutes it takes for all oranges to become rotten, or if it's impossible. Key pitfalls include properly handling edge cases such as no fresh oranges or no rotten oranges, and efficiently tracking the number of fresh oranges remaining after each minute of infection.

Solution Summary

The best solution to solve the problem of Rotting Oranges involves using Breadth-First Search (BFS) algorithm. Here is a step-by-step summary of the solution:
1. Initialize a queue to keep track of the rotten oranges and their levels.
2. Iterate through the grid to find all the initial rotten oranges and add them to the queue.
3. Perform BFS by repeatedly removing an orange from the queue, checking its neighboring oranges, and marking them as rotten if they are fresh.
4. Keep track of the time taken to rot all the oranges and the number of fresh oranges left.
5. Return the total time taken to rot all the oranges, considering that it is impossible to rot all oranges if there are still fresh ones left.
This solution efficiently finds the minimum time required to rot all the oranges using BFS traversal and considering the levels of the oranges in the grid.

Tags

Array

Breadth-First Search

Matrix