题目如下:

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. find按照文件大小查找

    例如,find -size +1M:查找大于 1 MB 的文件.其他参数: b: 512-byte blocks. This is the default if no unit is specifie ...

  2. Gene Ontology (GO) 注释

    Gene Ontology (GO) 注释  Posted on 2017-06-11 |  In 生信 相似的基因在不同物种中,其功能往往保守的.显然,需要一个统一的术语用于描述这些跨物种的同源基因 ...

  3. Appium API文档中文版

    传送门  https://testerhome.com/topics/3144

  4. Ext js-02 -官方API文档使用

    官方API文档地址: http://docs.sencha.com/extjs/6.5.3/classic/Ext.html 打开网页如下: 1.选择所使用的Ext js版本,后面offline do ...

  5. Java Web学习总结(9)学习总结-JSTL

    一,JSTL概述 JSTL(JSP Standard Tag Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能.jstl出现的目的同el一样也是要代替jsp页 ...

  6. macOS 和 Linux 的内核区别

    有些人可能会认为 macOS 和 Linux 内核之间存在相似之处,因为它们可以处理类似的命令和类似的软件.有些人甚至认为苹果公司的 macOS 是基于 Linux 的.事实上是,两个内核有着截然不同 ...

  7. NAGIOS(网络监视工具)

    Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机,路由器等网络设备,打印机等.在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员 ...

  8. Radical and array

    Radical and array 时间限制: 1 Sec  内存限制: 128 MB提交: 46  解决: 27[提交][状态] 题目描述 Radical has an array , he wan ...

  9. Hbuild X 打包 混合式app

    1.调用原生方法的接口文档:http://ask.dcloud.net.cn/docs/ (亲测有效,使用js 调用原生的设备方法是可以的) 2.HbuildX 可以直接真机调试的.(亲测有效,安装好 ...

  10. [bzoj3462]DZY Loves Math II (美妙数学+背包dp)

    Description Input 第一行,两个正整数 S 和 q,q 表示询问数量. 接下来 q 行,每行一个正整数 n. Output 输出共 q 行,分别为每个询问的答案. Sample Inp ...