LeetCode Rotting Oranges Problem Solved Using Multi-Source BFS Approach
The Rotting Oranges problem on LeetCode involves an m×n grid of fresh and rotten oranges, where every rotten orange spreads to adjacent fresh oranges each minute, and the goal is to find the minimum time to rot all oranges. A brute-force approach repeatedly scans the entire grid each minute, but this results in O((M×N)²) time complexity due to redundant cell visits. The optimal solution uses Multi-Source BFS, where all initially rotten oranges are pushed into a queue simultaneously at time zero and spread level by level. Each BFS level represents one minute, reducing time and space complexity to O(M×N). If any fresh oranges remain unreachable after BFS completes, the function returns -1 to indicate the task is impossible.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in