Advertisement

Rotting Oranges - LeetCode 994 Solution

Rotting Oranges - Complete Solution Guide

Rotting Oranges is LeetCode problem 994, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an m x n grid where each cell can have one of three values: 0 representing an empty cell, 1 representing a fresh orange, or 2 representing a rotten orange. 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 . Example 1: Input: grid = [[2,1,1],[1,1,0],[0,1,1]] Output: 4 Example 2: Input: grid = [[2,1,1],[0,1,1],[1,0

Detailed Explanation

The problem asks us to find the minimum time (in minutes) it takes for all fresh oranges in a grid to rot. The grid contains empty cells (0), fresh oranges (1), and rotten oranges (2). Every minute, a fresh orange that is adjacent (4-directionally: up, down, left, right) to a rotten orange becomes rotten itself. If it's impossible for all fresh oranges to rot, we should return -1. If there are no fresh oranges initially, the answer is 0.

Solution Approach

The solution uses Breadth-First Search (BFS) to simulate the rotting process. It starts by identifying all the rotten oranges and adding their coordinates along with an initial time of 0 to a queue. Then, the algorithm processes the queue level by level. For each rotten orange, it checks its four adjacent cells. If a fresh orange is found, it's marked as rotten, the number of fresh oranges is decremented, and the new rotten orange is added to the queue with an incremented time. The algorithm continues until the queue is empty. Finally, it checks if any fresh oranges remain. If so, it returns -1; otherwise, it returns the maximum time elapsed during the rotting process.

Step-by-Step Algorithm

  1. Step 1: Initialize variables: `rows`, `cols` (dimensions of the grid), `q` (a queue to store rotten oranges and their rotting time), `fresh_oranges` (count of fresh oranges).
  2. Step 2: Iterate through the grid and count the number of fresh oranges. Add the coordinates and initial time (0) of all rotten oranges to the queue `q`.
  3. Step 3: If `fresh_oranges` is 0, return 0 (no oranges to rot).
  4. Step 4: Initialize `minutes_elapsed` to 0.
  5. Step 5: Perform BFS: While the queue `q` is not empty, dequeue an element (row, col, minutes). Update `minutes_elapsed` to be the maximum of its current value and the `minutes` value dequeued.
  6. Step 6: For each of the four possible directions (up, down, left, right) from the current cell (row, col), check if the adjacent cell is within the grid boundaries and contains a fresh orange.
  7. Step 7: If an adjacent fresh orange is found, mark it as rotten (set its value to 2), decrement `fresh_oranges`, and enqueue the coordinates of the newly rotten orange and the incremented time (minutes + 1).
  8. Step 8: After the BFS traversal, if `fresh_oranges` is still greater than 0, it means some fresh oranges could not be rotted. Return -1.
  9. Step 9: Otherwise, return `minutes_elapsed` (the maximum time it took for all possible oranges to rot).

Key Insights

  • Insight 1: Breadth-First Search (BFS) is the ideal algorithm for this problem because we need to find the shortest time for the oranges to rot, and BFS explores the grid level by level.
  • Insight 2: The key to efficiently tracking time is to store the time elapsed along with the coordinates of the rotten oranges in the queue.
  • Insight 3: We need to keep track of the number of fresh oranges at the start and decrement it as they rot. This helps us determine if all fresh oranges have rotted at the end. Handling edge cases where no fresh oranges are present initially is important.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(m*n)

Topics

This problem involves: Array, Breadth-First Search, Matrix.

Companies

Asked at: Anduril, ByteDance, Citadel, DP world, Deutsche Bank, Docusign, Expedia, Flipkart, Informatica, Intuit, Lyft, Myntra, Nutanix, PhonePe, Roblox, Salesforce, Samsung, ServiceNow, VMware, Wix, X, Zepto, ZipRecruiter, Zoox, eBay.