[leetcode] 994. Rotting Oranges
题目
You are given an m x n grid where each cell can have one of three values:
0representing an empty cell,1representing a fresh orange, or2representing 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.lengthn == grid[i].length1 <= m, n <= 10grid[i][j]is0,1, or2.
思路
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的更多相关文章
- [LeetCode] 994. Rotting Oranges 腐烂的橘子
题目: 思路: 每个腐烂的橘子都能将自己上下左右的新鲜橘子传染,像极了现在的肺炎... 如果格子中只有一个腐烂的橘子,那么这便是一个典型的层次遍历,第一个传染多个,称为第二层,第二层传染第三层 但这里 ...
- 【Leetcode_easy】994. Rotting Oranges
problem 994. Rotting Oranges 参考 1. Leetcode_easy_994. Rotting Oranges; 完
- 【LeetCode】994. Rotting Oranges 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetco ...
- 【leetcode】994. Rotting Oranges
题目如下: In a given grid, each cell can have one of three values: the value 0 representing an empty cel ...
- Leetcode之广度优先搜索(BFS)专题-994. 腐烂的橘子(Rotting Oranges)
Leetcode之广度优先搜索(BFS)专题-994. 腐烂的橘子(Rotting Oranges) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ar ...
- Rotting Oranges - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Rotting Oranges - LeetCode 注意点 解法 解法一:bfs.首先先统计所有新鲜的橘子数目fresh,如果fresh大于0则一直执行 ...
- [Swift]LeetCode994. 腐烂的橘子 | Rotting Oranges
In a given grid, each cell can have one of three values: the value 0 representing an empty cell; the ...
- leetcode 994.腐烂的橘子
题目: 在给定的网格中,每个单元格可以有以下三个值之一: 值 0 代表空单元格: 值 1 代表新鲜橘子: 值 2 代表腐烂的橘子. 每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜橘子都会腐烂 ...
- 算法与数据结构基础 - 广度优先搜索(BFS)
BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数 ...
随机推荐
- Java自增自减运算
自增自减运算 //++(自增) --(自减) 一元允运算 int a =3; //a = a+1-----4 int b=a++; //执行完这行代码后,先给b赋值,再自增 System.out.pr ...
- 如何将原生微信小程序页面改成原生VUE框架的H5页面
项目背景: 公司为了实现小程序与H5页面共同覆盖,全面推广.特此想将已有的小程序进行快速改造上线(二周内),研发出H5版本.目前公司前端技术较为薄弱,现有的技术解决方案还停留在JSP. 问题: 如何将 ...
- Java 多线程:锁(一)
Java 多线程:锁(一) 作者:Grey 原文地址: 博客园:Java 多线程:锁(一) CSDN:Java 多线程:锁(一) CAS 比较与交换的意思 举个例子,内存有个值是 3,如果用 Java ...
- 【开源打印组件】vue-plugin-hiprint初体验
vue-plugin-hiprint的学习与应用 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习 ...
- Pod 使用进阶
静态 Pod 在 Kubernetes 集群中除了我们经常使用到的普通的 Pod 外,还有一种特殊的 Pod,叫做Static Pod,也就是我们说的静态 Pod,静态 Pod 有什么特殊的地方呢? ...
- 使用logstash拉取MySQL数据存储到es中的再次操作
使用情况说明: 已经使用logstash拉取MySQL数据存储到es中,es中也创建了相应的索引,也存储了数据.假若把这个索引给删除了,再次进行同步操作的话要咋做,从最开始的数据进行同步,而不是新增的 ...
- 开启tcp_timestamps和tcp_tw_recycle造成NAT转发连接不上
文章转载自:https://segmentfault.com/a/1190000022264813
- google浏览器个人常用快捷键
分享一下个人常用快捷键. 说明:字母排序规则遵循字母表(a->z) 快捷键 介绍 ctrl+0 恢复页面到100% ctrl+数字(1~9) 切换至序号对应的标签页 ctrl+d 将当前标签页添 ...
- C#-7 结构和枚举
一 什么是结构 结构是程序员定义的数据类型,有数据成员和函数成员,和类非常类似. 类是引用类型,而结构是值类型: 结构和类一样可以实现接口: 结构是隐式密封的,不能被派生: 结构类型的变量不能为nul ...
- Jquery封装的ajax的使用过程发生的问题
Jquery封装的ajax的使用过程发生的问题 今天在做项目的时候使用到了ajax来完成项目前后端数据交互,在之后发现在前端没有数据显示,而后端数据确实存在,在多次检查代码之后,发现代码并不存在问题, ...