Tap here to switch tabs
Problem
Submissions

Problem: Rotting Oranges

med
30 min
Explore how to determine the minimum time required for all fresh oranges in a grid to become rotten by understanding the problem constraints and applying efficient algorithmic techniques. This lesson helps you practice problem-solving with grid traversal and breadth-first search, focusing on algorithm efficiency within set constraints.

Statement

Consider an m×nm \times n grid containing cells with three potential values:

  • 00, which indicates an unoccupied cell.

  • 11, representing a freshly picked orange.

  • 22, indicating a rotten orange.

Any fresh orange that is 4–directionally adjacent to a rotten orange will also turn rotten with each passing minute.

Your task is to determine the minimum time required for all cells to have rotten oranges. In case, this objective cannot be achieved, return 1-1.

Constraints:

  • m == grid.length

  • n == grid[i].length

  • 11 \leq m, n \leq 1010

  • grid[i][j] is 01, or 2.

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths
Tap here to switch tabs
Problem
Submissions

Problem: Rotting Oranges

med
30 min
Explore how to determine the minimum time required for all fresh oranges in a grid to become rotten by understanding the problem constraints and applying efficient algorithmic techniques. This lesson helps you practice problem-solving with grid traversal and breadth-first search, focusing on algorithm efficiency within set constraints.

Statement

Consider an m×nm \times n grid containing cells with three potential values:

  • 00, which indicates an unoccupied cell.

  • 11, representing a freshly picked orange.

  • 22, indicating a rotten orange.

Any fresh orange that is 4–directionally adjacent to a rotten orange will also turn rotten with each passing minute.

Your task is to determine the minimum time required for all cells to have rotten oranges. In case, this objective cannot be achieved, return 1-1.

Constraints:

  • m == grid.length

  • n == grid[i].length

  • 11 \leq m, n \leq 1010

  • grid[i][j] is 01, or 2.

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths