【leetcode】994. Rotting Oranges
题目如下:
In a given grid, each cell can have one of three values:
- the value
0representing an empty cell;- the value
1representing a fresh orange;- the value
2representing 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
-1instead.Example 1:
Input: [[2,1,1],[1,1,0],[0,1,1]]
Output: 4Example 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 <= grid.length <= 101 <= grid[0].length <= 10grid[i][j]is only0,1, or2.
解题思路:采用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的更多相关文章
- 【LeetCode】994. Rotting Oranges 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetco ...
- 【Leetcode_easy】994. Rotting Oranges
problem 994. Rotting Oranges 参考 1. Leetcode_easy_994. Rotting Oranges; 完
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
随机推荐
- VS2015 Bad Request解决方法
新获取的项目,使用vs2015启动项目,遇到只能用localhost:xxxx的方式访问,使用192.168.**.**:xxxx这样ip+端口的方式无法访问的情况 原因:vs没有做出相应的配置 解决 ...
- Vue学习笔记-组件通信-父传子(props中的驼峰标识)
在组件中,使用选项props来声明需要从父级接收到的数据.props的值有两种方式:方式一:字符串数组,数组中的字符串就是传递时的名称.方式二:对象,对象可以设置传递时的类型,也可以设置默认值等. & ...
- linux学习-linux基础和帮助
1.终端terminal (1)设备终端 键盘,鼠标,显示器 (2)物理终端(/dev/console) 控制台concole (3)串行终端(/dev/ttyS#) ttyS (4)虚拟终端(tty ...
- shell脚本学习(4)cut
cut 的两种用法 1种是 -c list 剪切字符串中特定位置的文字, /etc/passwd中的原始数据: yuyuyu:x:1000:1000:yuyuyu,,,:/home/yuyuyu: ...
- POJ 3348 Cows (凸包模板+凸包面积)
Description Your friend to the south is interested in building fences and turning plowshares into sw ...
- CSS中的flex布局
1.flex 布局的概念 Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性.任何一个容器都可以指定为 Flex 布局,行内元素也可以通过 ...
- Windows Server服务器之用户界面,任务管理器等
用户界面简化服务器管理.跟Windows 8一样,重新设计了服务器管理器,采用了Metro界面(核心模式除外).在这个Windows系统中,PowerShell已经有超过2300条命令开关(Windo ...
- HihoCoder 1055 刷油漆 (树上背包)
题目:https://vjudge.net/contest/323605#problem/A 题意:一棵树,让你选择m个点的一个连通块,使得得到的权值最大 思路:树上背包,我们用一个dp数组,dp[i ...
- 2018-2019-2 20175307实验三《敏捷开发与XP实践》实验报告
实验三 敏捷开发与XP实践-1 1.仔细学习了http://www.cnblogs.com/rocedu/p/4795776.html,发布了一篇关于Google的Java编码的博客,具体内容就不在这 ...
- java并发编程笔记(六)——AQS
java并发编程笔记(六)--AQS 使用了Node实现FIFO(first in first out)队列,可以用于构建锁或者其他同步装置的基础框架 利用了一个int类型表示状态 使用方法是继承 子 ...
