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.

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.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12169911.html

时间: 2024-08-30 15:04:26

LeetCode 994. Rotting Oranges的相关文章

【Leetcode_easy】994. Rotting Oranges

problem 994. Rotting Oranges 参考 1. Leetcode_easy_994. Rotting Oranges; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11316970.html

[LC] 994. 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

Leetcode之广度优先搜索(BFS)专题-994. 腐烂的橘子(Rotting Oranges)

BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree Level Order Traversal) 在给定的网格中,每个单元格可以有以下三个值之一: 值 0 代表空单元格: 值 1 代表新鲜橘子: 值 2 代表腐烂的橘子. 每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜橘子都会腐烂. 返回直到单元格中没有新鲜橘子为止所必须经过的最小分钟数.如果不可能,返回 -1. 示例 1: 输入:[[2,1,1],[1,1,0],[0,1,1

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

目录 题目描述: 示例 1: 示例 2: 示例 3: 解法: 题目描述: 在给定的网格中,每个单元格可以有以下三个值之一: 值 0 代表空单元格: 值 1 代表新鲜橘子: 值 2 代表腐烂的橘子. 每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜橘子都会腐烂. 返回直到单元格中没有新鲜橘子为止所必须经过的最小分钟数.如果不可能,返回 -1. 示例 1: 输入:[[2,1,1],[1,1,0],[0,1,1]] 输出:4 示例 2: 输入:[[2,1,1],[0,1,1],[1,0,1]]

leetcode.994腐烂的橘子

在给定的网格中,每个单元格可以有以下三个值之一: 值 0 代表空单元格:值 1 代表新鲜橘子:值 2 代表腐烂的橘子.每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜橘子都会腐烂. 返回直到单元格中没有新鲜橘子为止所必须经过的最小分钟数.如果不可能,返回 -1. 示例 1: 输入:[[2,1,1],[1,1,0],[0,1,1]]输出:4示例 2: 输入:[[2,1,1],[0,1,1],[1,0,1]]输出:-1解释:左下角的橘子(第 2 行, 第 0 列)永远不会腐烂,因为腐烂只会发

LeetCode——994. 腐烂的橘子

994. 腐烂的橘子 在给定的网格中,每个单元格可以有以下三个值之一: 值 0 代表空单元格: 值 1 代表新鲜橘子: 值 2 代表腐烂的橘子. 每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜橘子都会腐烂. 返回直到单元格中没有新鲜橘子为止所必须经过的最小分钟数.如果不可能,返回 -1. 示例 1: 输入:[[2,1,1],[1,1,0],[0,1,1]] 输出:4 示例 2: 输入:[[2,1,1],[0,1,1],[1,0,1]] 输出:-1 解释:左下角的橘子(第 2 行, 第

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

1 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 2 class Solution 3 { 4 public: 5 int orangesRotting(vector<vector<int>>& grid) 6 { 7 int left = 0; 8 int sz1 = grid.size(); 9 int sz2 = grid[0].size(); 10 _for(i,0,sz1) 11 _for(j,0,sz2

leetcode 994.腐烂的橘子

题目: 在给定的网格中,每个单元格可以有以下三个值之一: 值 0 代表空单元格: 值 1 代表新鲜橘子: 值 2 代表腐烂的橘子. 每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜橘子都会腐烂. 返回直到单元格中没有新鲜橘子为止所必须经过的最小分钟数.如果不可能,返回 -1. 分析: 最近在看广度优先搜素的题目,这个是比较简单基础的题了. 腐烂的橘子会把靠近他的新鲜的橘子腐蚀,那么就是只要从所有坏的橘子的地方一层一层往外遍历就可以了. 代码: 1 //5ms 97% 2 class So

算法与数据结构基础 - 广度优先搜索(BFS)

BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数据结构基础 - 队列(Queue) 最直观的BFS应用是图和树的遍历,其中图常用邻接表或矩阵表示,例如 LeetCode题目 690. Employee Importance: // LeetCode 690. Employee Importance/* class Employee { publi