Rotting Oranges
Problem Statement
You are given a grid representing a pile of oranges, where each cell in the grid can be one of three states: fresh orange, rotten orange, or empty cell. A fresh orange at position (i, j) can become rotten in the next minute if there is a rotten orange in the same row or column with either index i or j. The goal is to determine the minimum number of minutes needed to rot all fresh oranges, which is equivalent to finding the earliest time at which all oranges can become rotten.
Rules and Constraints
- The input grid is a 2D array of integers, where each integer is a state:
- 0 indicates an empty cell or a clean-up area
- 1 represents a fresh orange
- 2 represents a rotten orange
- The grid is guaranteed to have at least one fresh orange.
- The grid size can be anywhere from 1x1 to 100x100.
- Each cell in the grid can be visited at most once.
- The input grid is not necessarily a square grid.
Time and Space Complexity Constraints
- The algorithm should have a time complexity of O(R * C), where R is the number of rows and C is the number of columns in the grid.
- The algorithm should have a space complexity of O(R * C), which is the space required to store the visited status of each cell in the grid.
- The algorithm should not modify the original grid.
Please note that these complexity constraints are applicable for the main algorithm execution. The input and output operations are not included in these constraints.
Example
Input: {"input_data":[1,2,3]}
Output: [1,2,3]