[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见:算法与数 ...
随机推荐
- 三 单例模式【Singleton Pattern】 来自CBF4LIFE 的设计模式
这个模式是很有意思,而且比较简单,但是我还是要说因为它使用的是如此的广泛,如此的有人缘,单例就是单一.独苗的意思,那什么是独一份呢?你的思维是独一份,除此之外还有什么不能山寨的呢?我们举个比较难复制的 ...
- 配置IConfiguration
前言 配置是我们必不可少的功能,我们在开发中,经常会遇到需要获取配置信息的需求,那么如何才能优雅的获取配置信息? 我们希望新的配置: 支持强类型 配置变更后通知 学习难度低 快速入门 根据使用场景我们 ...
- C# 开发过程中常见错误记录及解决说明
1.异常了类型: 1.1.1.1 异常错误信息:An error occurred while updating the entries. See the inner exception for de ...
- [Python]-tqdm模块-给for循环加上进度条
import tqdm 使用tqdm模块,可以在漫长的for循环加上一个进度条,显示当前进度百分比. 将tqdm写在迭代器之外即可:tqdm(iterator) for i in tqdm(range ...
- 接入Twitter和Facebook分享踩坑记录
准备工作 1.首先需要在HTML的head添加下述meta标签内容,在分享时,Twitter和Facebook会爬取该网站页面的meta内容,然后生成分享卡片. 2.按照下述配置完成后,需要把内容发布 ...
- MinIO多租户(Multi-tenant)部署指南
官方文档地址:http://docs.minio.org.cn/docs/master/multi-tenant-minio-deployment-guide 单机部署 在单台机器上托管多个租户,为每 ...
- 路径参数:Path Parameters
官方文档地址:https://fastapi.tiangolo.com/zh/tutorial/path-params/ # -*- coding: UTF-8 -*- from fastapi im ...
- ELK基于ElastAlert实现日志的微信报警 ---docker环境
参考网址:https://github.com/anjia0532/elastalert-docker 1.拉取镜像: docker pull anjia0532/elastalert-docker: ...
- SpringBoot 项目部署(初级)
之前的项目一直在本地电脑上写,最近需要将项目部署到服务器上进行联调测速度.于是,在网上搜集资料后简单的进行一下总结. 由于本次打包部署是为了测试,于是很多内容做的还不算详尽,只是将项目简单的打包为ja ...
- POJ3260 The Fewest Coins(混合背包)
支付对应的是多重背包问题,找零对应完全背包问题. 难点在于找上限T+maxv*maxv,可以用鸽笼原理证明,实在想不到就开一个尽量大的数组. 1 #include <map> 2 #inc ...