题目如下:

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. Zabbix微信报警脚本及写触发记录

    一.Zabbix微信报警 默认路径:/usr/lib/zabbix/scripts/weixin.py #!/usr/bin/python # -*- coding: utf-8 -*- import ...

  2. pytorch数据预处理错误

    出错: Traceback (most recent call last): File , in <module> train_model(model_conv, criterion, o ...

  3. cocos2D-X Doc

    { //https://docs.cocos2d-x.org/api-ref/index.html //https://docs.cocos.com/cocos2d-x/manual/zh/sprit ...

  4. Linux如何查看进程是否存活

    ps  -ef  | grep nginx ps -ef | grep(过滤) 进程名字

  5. 天池平台再升级,打造产业AI知识共享、技术共享平台

    在5月23日的云栖大会·武汉峰会上,天池发布“全球AI开发者计划”,打造一站式人工智能知识共享平台,计划2年内在平台上集聚30万AI工程师.同时,阿里云天池正式升级,成为从产业机会到实施交付一站式解决 ...

  6. flutter动态列表

    在前面一篇总结flutter里面的列表组件ListView的时候,一直都是使用的静态数据,但是在实际的运用过程中,数据都是从后台获取的动态数据,不能再像前面那样写静态数据了,下面模拟一下如果使用动态数 ...

  7. ansible控制winserver笔记

    原文地址: https://www.cnblogs.com/kingleft/p/6391652.html 环境描述: ansible控制远程windows .系统必须是sp1 .安装framewor ...

  8. 建站手册-浏览器信息:苹果 Safari 浏览器

    ylbtech-建站手册-浏览器信息:苹果 Safari 浏览器 1.返回顶部 1. http://www.w3school.com.cn/browsers/browsers_safari.asp 2 ...

  9. (64)C# 预处理器指令

    预处理器命令从来不会被翻译为可执行代码中的命令,但会影响编译过程的各个方面.例如:使用预处理器指令可以禁止编译器编译代码的某一部分,如果计划发布两个版本的代码,即基本版本和有更多功能的企业版本,即可以 ...

  10. Numpy的基本运算及操作

    import numpy as np ''' 一.算术运算 元素级 1.标量 加减乘除 数组(元素级:位置对应) 自增和自减 通用函数 2.数组 +-*/ 数组 (元素级) 3.条件和布尔运算 a&g ...