题目如下:

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.

解题思路:采用BFS的思想,依次把坏的橘子附近的好的橘子变成坏的橘子,求出最大值。有一点要注意的是,有些好橘子四周都是空格的话,这个橘子不会变坏。因此最后要计算剩余好橘子的数量,以此确定是否返回-1。

代码如下:

class Solution(object):
def orangesRotting(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
queue = []
#visit = []
fresh_count = 0
for i in range(len(grid)):
#visit.append([0]*len(grid[i]))
for j in range(len(grid[i])):
if grid[i][j] == 2:
queue.append((i,j,0))
elif grid[i][j] == 1:
fresh_count += 1
res = 0
while len(queue) > 0:
x,y,c = queue.pop(0)
direction = [(0,1),(0,-1),(1,0),(-1,0)]
for (i,j) in direction:
if x + i >= 0 and x + i < len(grid) and y+j >=0 and y+j < len(grid[0]) and grid[x+i][y+j] == 1:
queue.append((x+i,y+j,c+1))
grid[x + i][y + j] = 2
res = max(res,c+1)
fresh_count -= 1
if fresh_count > 0:
return -1
return res

【leetcode】994. Rotting Oranges的更多相关文章

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

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

  2. 【Leetcode_easy】994. Rotting Oranges

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

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. JAVA学习笔记--赋值(“=”)

    参考来源:<java编程思想(第四版)> 见第三章3.4节 基本数据类型存储了实际的数值,并非指向一个对象的引用,故其赋值,就是直接将一个地方的内容复制到了另一个地方.例如,对基本数据类型 ...

  2. ubuntu软件源变更阿里源和arm板子变更国内源

    使用 lsb_release -c 命令,看看当前版本号,替换一下“bionic”字符 1.更改 /etc/apt/sources.list,更改之前先备份一下 deb http://mirrors. ...

  3. web前端到底怎么学?

    互联网+的火爆,让互联网行业快速的扩张.越来越多的人想通过学习的途径进入这个行业,java开发.WEB前端开发.UI设计等专业受到大众追捧.小编这次主要介绍一下WEB前端开发,为想要学习web前端开发 ...

  4. 【CF1210D】Konrad and Company Evaluation(vector,图论)

    题意:有i个人,m对两两之间的关系,第i个人初始的薪水为i,有q次操作,第i次操作会把v[i]号的薪水提升成n+i 如果两个人之间存在关系,薪水高的会向薪水低的炫耀 定义u,v,w为一个三元组,当u向 ...

  5. KVM(多电脑切换器)

    KVM:Keyboard Video Mouse的缩写.KVM 交换机通过直接连接键盘.视频和鼠标 (KVM) 端口,让您能够访问和控制计算机.KVM 技术无需目标服务器修改软件.这就意味着可以在 W ...

  6. window使用

    运行命令 regedit #注册表编译器 firewall.cpl #打开防火墙配置 powershell #打开powershell control #打开控制面版 cnpa.cpl #打开网络设置 ...

  7. [CSP-S模拟测试]:画作(BFS+数学)

    题目描述 小$G$的喜欢作画,尤其喜欢仅使用黑白两色作画.画作可以抽象成一个$r\times c$大小的$01$矩阵.现在小$G$构思好了了他的画作,准备动笔开始作画.初始时画布是全白的,他每一次下笔 ...

  8. 颁发不受浏览器信任的SSL证书

    xshell登录服务器,使用openssl生成RSA密钥及证书 # 生成一个RSA密钥 $ openssl genrsa -des3 -out tfjybj.key 1024 # 生成一个证书请求$ ...

  9. Java学习之Thread方法

    一.sleep()方法:在同步中,释放CPU执行权,不释放同步锁,意味着程序停止 二.停止线程方法:原理:run执行完成 1.设置标记 class StopThread implements Runn ...

  10. Iconv作用以及安装问题解决

    当我们在使用Window操作系统的时候,可能使用最多的文本格式就是txt了,但是当我们将Window平台下的txt文本文档拷贝到Linux平台下查看时,发现原来的中文全部变成了乱码.没错, 引起这个结 ...