题目

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,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: grid = [[0,2]]
Output: 0
Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.

Constraints:

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

思路

bfs模拟橘子变质的过程。

代码

python版本:

from collections import deque

class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
res = 0
q = deque()
[q.append((i, j, 0)) for i in range(len(grid)) for j in range(len(grid[0])) if grid[i][j] == 2]
while len(q):
i, j, count = q.popleft()
if grid[i][j] == 2 and count:
continue
grid[i][j] = 2
res = max(res, count)
for l, r in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
if 0 <= i+l < len(grid) and 0 <= j+r < len(grid[0]) and grid[i+l][j+r] == 1:
q.append((i+l, j+r, count+1))
has = any([j == 1 for i in grid for j in i])
return -1 if has else res

[leetcode] 994. Rotting Oranges的更多相关文章

  1. [LeetCode] 994. Rotting Oranges 腐烂的橘子

    题目: 思路: 每个腐烂的橘子都能将自己上下左右的新鲜橘子传染,像极了现在的肺炎... 如果格子中只有一个腐烂的橘子,那么这便是一个典型的层次遍历,第一个传染多个,称为第二层,第二层传染第三层 但这里 ...

  2. 【Leetcode_easy】994. Rotting Oranges

    problem 994. Rotting Oranges 参考 1. Leetcode_easy_994. Rotting Oranges; 完

  3. 【LeetCode】994. Rotting Oranges 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetco ...

  4. 【leetcode】994. Rotting Oranges

    题目如下: In a given grid, each cell can have one of three values: the value 0 representing an empty cel ...

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

    Leetcode之广度优先搜索(BFS)专题-994. 腐烂的橘子(Rotting Oranges) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ar ...

  6. Rotting Oranges - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Rotting Oranges - LeetCode 注意点 解法 解法一:bfs.首先先统计所有新鲜的橘子数目fresh,如果fresh大于0则一直执行 ...

  7. [Swift]LeetCode994. 腐烂的橘子 | Rotting Oranges

    In a given grid, each cell can have one of three values: the value 0 representing an empty cell; the ...

  8. leetcode 994.腐烂的橘子

    题目: 在给定的网格中,每个单元格可以有以下三个值之一: 值 0 代表空单元格: 值 1 代表新鲜橘子: 值 2 代表腐烂的橘子. 每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜橘子都会腐烂 ...

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

    BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数 ...

随机推荐

  1. Go 语言入门 3-动态数组(slice)的特性及实现原理

    go 语言中的动态数组(slice),是基于数组实现的,可以相比数组而言更加的灵活.其他语言的 slice 通常仅是一个 API, 但是 go 语言的 slice 不仅仅是一种操作, 也是一种数据结构 ...

  2. IDEA整合Docker

    创建项目 1.使用 IDEA 构建一个 SpringBoot 项目 2.编写一个helloController @RestController public class HelloController ...

  3. 如何修改SAO用户密码

    KingbaseES SAO 用户是专门用于审计管理的用户,用户配置审计策略需要使用该用户.在initdb 完成后,SAO  用户的默认密码保存在参数 sysaudit.audit_table_pas ...

  4. 操作系统学习笔记10 | I/O、显示器与键盘

    从这一部分开始介绍操作系统的设备驱动,操作系统通过文件系统的抽象驱动设备让用户能够使用显示器.键盘等交互工具.并讲解printf和scanf是如何实现敲下键盘将字符显示到屏幕上的. 参考资料: 课程: ...

  5. MAC MySQL安装配置

    1. 下载 下载地址:https://dev.mysql.com/downloads/mysql/ 注意选择对应的版本,M系列芯片对应ARM 2. 安装 参考官网教程, 点击地址查看, 一直点击继续即 ...

  6. 2022第五空间-web部分wp+复盘总结

    打了一天,麻了,大佬tql,这次get到了不少东西,一是一个不太常见的宽字节注入,我是真的没想到,而且后面也是看了wp理解了好一会才弄明白. 0x01: 题目是一个登录框,但是基本上是过滤掉了所有的常 ...

  7. 微服务系列之授权认证(三) JWT

    1.JWT简介 官方定义:JWT是JSON Web Token的缩写,JSON Web Token是一个开放标准(RFC 7519),它定义了一种紧凑的.自包含的方式,可以将各方之间的信息作为JSON ...

  8. Nginx反代服务器基础配置实践案例

    转载自:https://www.bilibili.com/read/cv16149433?spm_id_from=333.999.0.0 方式1: 轮询 RR(默认轮询)每个请求按时间顺序逐一分配到不 ...

  9. 第六章:Django 综合篇 - 4:django-admin和manage.py

    目录 一.Django内置命令选项 check dbshell diffsettings flush makemigrations migrate runserver shell startapp s ...

  10. Maven+SpringMVC+Dubbo 简单的入门demo配置

    转载自:https://cloud.tencent.com/developer/article/1010636 之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程 ...