题目如下:

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. Linux中Hard link和Symbol link的区别

    Hard link Hard link不能指向不在同一磁盘的文件 Hard link不能指向目录 Hard link与源文件几乎没有区别.只能通过ls -li看出link关系.另外,删除源文件后,Ha ...

  2. 第二周作业—N42-虚怀若谷

    一.描述Linux发行版的系统目录名称命名规则以及用途 1.文件名命名规则 (1).文件名最长255字节 (2).包括路径在内的文件名称最长4095个字节 (3).除了斜扛和NUL,所有字符都有效.但 ...

  3. 前端每日实战:84# 视频演示如何用纯 CSS 创作一个极品飞车 loader

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/MBbEMo 可交互视频 此视频是可 ...

  4. pytest_用例运行级别_模块级

    ''' pytest 参数说明 https://www.jianshu.com/p/7a7432340f02 -x test_fixt_model.py 遇到错误时,停止运行 用-v运行(-v显示运行 ...

  5. 北风设计模式课程---解释器模式(Interpreter Pattern)

    北风设计模式课程---解释器模式(Interpreter Pattern) 一.总结 一句话总结: 不仅要通过视频学,还要看别的博客里面的介绍,搜讲解,搜作用,搜实例 设计模式都是对生活的抽象,比如用 ...

  6. jmeter添加自定义扩展函数之大写转换小写

    1,打开eclipse,新建maven工程,在pom中引用jmeter核心jar包,具体请看---https://www.cnblogs.com/guanyf/p/10863033.html---,这 ...

  7. Oralce-PL/SQL编程-游标

    PL/SQL(Procedural Language/SQL)是Oracle在数据库中引入的一种过程化编程语言. PL/SQL块结构 声明部分 执行部分(必须的) 异常处理部分 [declare] - ...

  8. Tecplot 360 安装后弹出“Is your Tecplot 360 EX liense valid?”解决方法

    在hosts文件中添加127.0.0.1 download.tecplot.com这句指令时,应注意1与download之间有空格!

  9. 同步架构OR异步架构

    把智能系统比喻成KFC营业厅,处理器是窗口和窗口后面的服务员(把一个窗口当作一个核心),指令集是后面排队的人,窗口是数据吞吐量.当中午就餐人多的时候,一个窗口肯定忙不过来,这时候可以增加窗口,有两种方 ...

  10. oo_project_2java数据类型范围及测试

    数据类型范围测试 一.数字常数的编译问题 java中的常量数字默认以int型编译 如: long a = 1234567890; //十位 long b = 12345678900; //默认数据为i ...