Rotting Oranges

https://leetcode.com/problems/rotting-oranges/?envType=problem-list-v2&envId=7p5x763

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

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.

Screenshot 2026-01-31 at 1.55.49 PM.png

For Rotting Oranges, the cleanest solution is multi-source BFS (queue). Every rotten orange (2) is a starting point at time 0. Each minute is one BFS “layer”, and rotting spreads to 4-directional neighbors.

Why BFS is the right tool

What’s going wrong in your rough approach

Correct BFS algorithm

  1. Count all fresh oranges (fresh).
  2. Push all rotten orange positions into a queue with minute 0.