Class Solution

java.lang.Object
g0901_1000.s0994_rotting_oranges.Solution

public class Solution extends Object
994 - Rotting Oranges.<p>Medium</p> <p>You are given an <code>m x n</code> <code>grid</code> where each cell can have one of three values:</p> <ul> <li><code>0</code> representing an empty cell,</li> <li><code>1</code> representing a fresh orange, or</li> <li><code>2</code> representing a rotten orange.</li> </ul> <p>Every minute, any fresh orange that is <strong>4-directionally adjacent</strong> to a rotten orange becomes rotten.</p> <p>Return <em>the minimum number of minutes that must elapse until no cell has a fresh orange</em>. If <em>this is impossible, return</em> <code>-1</code>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2019/02/16/oranges.png" alt="" /></p> <p><strong>Input:</strong> grid = [[2,1,1],[1,1,0],[0,1,1]]</p> <p><strong>Output:</strong> 4</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> grid = [[2,1,1],[0,1,1],[1,0,1]]</p> <p><strong>Output:</strong> -1</p> <p><strong>Explanation:</strong> The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> grid = [[0,2]]</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> Since there are already no fresh oranges at minute 0, the answer is just 0.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 <= m, n <= 10</code></li> <li><code>grid[i][j]</code> is <code>0</code>, <code>1</code>, or <code>2</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • orangesRotting

      public int orangesRotting(int[][] grid)