Rotting Oranges Problem

Rotting Oranges Problem — ExecCode Medium DSA Practice

Solve the Rotting Oranges problem on ExecCode. Free online medium DSA practice in Graph. Write and run code in Java, C++, Python — no signup required to run.

Problem description

You are given an m×n grid where each cell can have: 0 empty, 1 fresh orange, or 2 rotten orange. Every minute, any fresh orange 4-directionally adjacent to a rotten orange becomes rotten. Return the minimum minutes until no fresh orange remains, or -1 if impossible.

Examples

Input grid = [[2, 1, 1], [1, 1, 0], [0, 1, 1]]; Output 4. Input grid = [[2, 1, 1], [0, 1, 1], [1, 0, 1]]; Output -1. Input grid = [[0, 2]]; Output 0

Constraints

1 ≤ m, n ≤ 10 grid[i][j] is 0, 1, or 2

Practice Rotting Oranges free on ExecCode. Browse DSA problems, topic map, and placement guides.