994. Rotting Oranges[Medium]

Posted Aaron

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了994. Rotting Oranges[Medium]相关的知识,希望对你有一定的参考价值。

994. Rotting Oranges

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.

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 10
  • grid[i][j] is 0, 1, or 2.

Example

Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
Output: 4

思路

先遍历一边二维数组,有两个目的

  • 统计新鲜橘子数量,到最后如果还有剩余的新鲜橘子,那就说明没有全部腐烂,直接返回-1
  • 把已经腐烂的橘子放进队列中,这就是首批腐烂橘子,对应着 res -〉0

接下来就是依次取出队列中腐烂橘子,做广度遍历,只要在边界范围内并且是新鲜橘子的,就将其腐烂,然后也入队列,每一批次队列的橘子取完,那这回合的腐烂就结束了,res++

题解

    Integer freshCount = 0;

    public int orangesRotting(int[][] grid) 
        int res;
        Queue<int[]> queue = new LinkedList<>();
        for (int i = 0; i < grid.length; i++) 
            for (int j = 0; j < grid[0].length; j++) 
                if (grid[i][j] == 1)
                    freshCount++;
                if (grid[i][j] == 2)
                    queue.add(new int[]i, j);
            
        
        res = bfs(grid, queue);
        return freshCount == 0 ? res : -1;
    

    public int bfs(int[][] grid, Queue<int[]> queue) 
        int res = 0;
        int[][] dire = new int[][]-1, 0, 1, 0, 0, -1, 0, 1;
        while (!queue.isEmpty() && freshCount != 0) 
            int size = queue.size();
            for (int j = 0; j < size; j++) 
                int[] curPos = queue.poll();
                for (int i = 0; i < dire.length; i++) 
                    int curRow = curPos[0] + dire[i][0];
                    int curCol = curPos[1] + dire[i][1];
                    if (curRow < 0 || curRow >= grid.length || curCol < 0 || curCol >= grid[0].length
                            || grid[curRow][curCol] != 1)
                        continue;
                    queue.add(new int[]curRow, curCol);
                    grid[curRow][curCol] = 2;
                    freshCount--;
                
            
            res++;
        
        return res;
    

LeetCode 994. Rotting Oranges

原题链接在这里:https://leetcode.com/problems/rotting-oranges/

题目:

In a given grid, each cell can have one of three values:

  • the value 0 representing an empty cell;
  • the value 1 representing a fresh orange;
  • the value 2 representing a rotten orange.

Every minute, any fresh orange that is adjacent (4-directionally) 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 instead.

Example 1:

Input: [[2,1,1],[1,1,0],[0,1,1]]
Output: 4

Example 2:

Input: [[2,1,1],[0,1,1],[1,0,1]]
Output: -1
Explanation:  The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.

Example 3:

Input: [[0,2]]
Output: 0
Explanation:  Since there are already no fresh oranges at minute 0, the answer is just 0.

Note:

  1. 1 <= grid.length <= 10
  2. 1 <= grid[0].length <= 10
  3. grid[i][j] is only 01, or 2.

题解:

Iterate grid, for rotten orange, add it to the queue, for fresh orange, count++.

Perform BFS, when neibor is fresh, mark it as rotton and add to que, count--.

If eventually count == 0, then all rotton. return level.

Note: pay attention to corner case. [[0]], at the beginning, count == 0, return 0.

Time Complexity: O(m * n). m = grid.length. n = grid[0].length.

Space: O(m * n).

AC Java:

 1 class Solution {
 2     public int orangesRotting(int[][] grid) {
 3         if(grid == null || grid.length == 0){
 4             return 0;
 5         }
 6         
 7         int m = grid.length;
 8         int n = grid[0].length;
 9         LinkedList<int []> que = new LinkedList<>();
10         int cnt = 0;
11         for(int i = 0; i < m; i++){
12             for(int j = 0; j < n; j++){
13                 if(grid[i][j] == 2){
14                     que.add(new int[]{i, j});
15                 }else if(grid[i][j] == 1){
16                     cnt++;
17                 }
18             }
19         }
20         
21         if(cnt == 0){
22             return 0;
23         }
24         
25         int [][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
26         int level = -1;
27         while(!que.isEmpty()){
28             level++;
29             int size = que.size();
30             while(size-- > 0){
31                 int [] cur = que.poll();
32                 grid[cur[0]][cur[1]] = 2;
33                 
34                 for(int [] dir : dirs){
35                     int x = cur[0] + dir[0];
36                     int y = cur[1] + dir[1];
37                     if(x < 0 || x >= m || y < 0 || y >= n || grid[x][y] != 1){
38                         continue;
39                     }
40                     
41                     grid[x][y] = 2;
42                     cnt--;
43                     que.add(new int[]{x, y});
44                 }
45             }
46         }
47         
48         return cnt == 0 ? level : -1;
49     }
50 }

类似Walls and GatesShortest Distance from All Buildings.

以上是关于994. Rotting Oranges[Medium]的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode_easy994. Rotting Oranges

[LC] 994. Rotting Oranges

[LeetCode] 994. Rotting Oranges

Leetcode 994. Rotting Oranges

Leetcode-994 Rotting Oranges(腐烂的橘子)

leetcode 994. 腐烂的橘子(Rotting Oranges)