Practice Rotting Oranges
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
The grid uses 0 for an empty cell, 1 for a fresh orange, and 2 for a rotten orange. In each minute, all currently rotten oranges infect fresh neighbors above, below, left, and right simultaneously. Find how many minutes are required until no fresh oranges remain. Return -1 if any fresh orange is unreachable, or 0 if none are fresh initially. Practice approach: Start breadth-first search with every initially rotten cell in the queue. Process one queue layer per minute while tracking the number of fresh oranges. Mark an orange when enqueuing it so it is not counted twice. Each grid cell is processed at most once: O(m*n) time and space.
Source: Rotting Oranges on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
grid = [[2,1,1],[1,1,0],[0,1,1]]
4
# Example 2
grid = [[2,1,1],[0,1,1],[1,0,1]]
-1
# Example 3
grid = [[0,2]]
0
Constraints
Related LeetCode Problems
Practice more problems similar to Rotting Oranges.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.