[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 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.

class Solution {
    public int orangesRotting(int[][] grid) {
        if (grid.length == 0 || grid[0].length == 0) {
            return 0;
        }
        int numOrange = 0;
        int row = grid.length;
        int col = grid[0].length;
        Queue<Cell> queue = new LinkedList<>();

        int res = 0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] == 2) {
                    queue.offer(new Cell(i, j, grid[i][j]));
                } else if (grid[i][j] == 1) {
                    numOrange += 1;
                }
            }
        }

        // no fresh orange
        if (numOrange == 0) {
            return 0;
        }

        int[] directX = {-1, 0, 0, 1};
        int[] directY = {0, -1, 1, 0};
        while(!queue.isEmpty()) {
            int size = queue.size();
            res += 1;
            for (int i = 0; i < size; i++) {
                Cell cur = queue.poll();
                for (int k = 0; k < 4; k++) {
                    int nextX = cur.x + directX[k];
                    int nextY = cur.y + directY[k];
                    if (nextX >= 0 && nextX < grid.length && nextY >= 0 && nextY < grid[0].length && grid[nextX][nextY] == 1) {
                        queue.offer(new Cell(nextX, nextY, grid[nextX][nextY]));
                        grid[nextX][nextY] = 2;
                        numOrange -= 1;
                        if (numOrange == 0) {
                            return res;
                        }
                    }
                }
            }
        }
        return -1;
    }
}

class Cell {
    int x;
    int y;
    int value;
    public Cell(int x, int y, int value) {
        this.x = x;
        this.y = y;
        this.value = value;
    }
}

原文地址:https://www.cnblogs.com/xuanlu/p/12388795.html

时间: 2024-08-05 16:29:15

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

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 minut

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之广度优先搜索(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 #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

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

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

Lc.exe已退出 代码为-1

今天使用vs2010开发,有人在vss项目中增加了一个第三方组件,后来删除了,我的计算机上没有这个第三方组件,结果导致了LC.exe错误:"Lc.exe已退出 代码为-1 " 解决方法: 1.把项目文件夹下Properties文件夹下的licenses.licx文件删除,重新编译即可: 2.文本方式打开*.csproj文件,在文件中查找licenses.licx字样,删除对应节点. 注意:还有一种情况就是Properties文件夹下已经没有licenses.licx文件了,程序还是报这

chkrootkit 编译报错的解决/usr/bin/ld: cannot find -lc

1:Centos6.5安装chkrootkit wget ftp://ftp.pangeia.com.br/pub/seg/pac/chkrootkit.tar.gz tar xvf chkrootkit.tar.gz cd chkrootkit-0.51/ make sense报错如下: /usr/bin/ld: cannot find -lc collect2: ld returned 1 exit status make: *** [strings-static] Error 1 2:解决

CodeForces 23C Oranges and Apples 抽屉原理

题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; #define inf 10000000 #define l