bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 994. Rotting Oranges

994. Rotting Oranges

Leetcode Solutions

Breadth-First Search (BFS) for Rotting Oranges

  1. Initialize a queue to keep track of rotten oranges and a counter for fresh oranges.
  2. Iterate over the grid, enqueue the position of all rotten oranges and count fresh oranges.
  3. If there are no fresh oranges, return 0 as no time is needed.
  4. Use a delimiter to separate minutes in the queue.
  5. While the queue is not empty, process oranges level by level (minute by minute).
  6. For each rotten orange, rot its fresh neighbors and enqueue them.
  7. After processing all oranges in a minute, increment the time.
  8. After BFS, if there are still fresh oranges, return -1; otherwise, return the time.
UML Thumbnail

In-place BFS for Rotting Oranges

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...