[LeetCode] 286. Walls and Gates_Medium tag: BFS
You are given a m x n 2D grid initialized with these three possible values.
-1- A wall or an obstacle.0- A gate.INF- Infinity means an empty room. We use the value231 - 1 = 2147483647to representINFas you may assume that the distance to a gate is less than2147483647.
Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.
Example:
Given the 2D grid:
INF -1 0 INF
INF INF INF -1
INF -1 INF -1
0 -1 INF INF
After running your function, the 2D grid should be:
3 -1 0 1
2 2 1 -1
1 -1 2 -1
0 -1 3 4 这个题目是用BFS来解决, 最naive approach就是扫每个点,然后针对每个点用BFS一旦找到0, 代替点的值. 时间复杂度为 O((m*n)^2);
我们还是用BFS的思路, 但是我们先把2D arr 扫一遍, 然后把是0的位置都append进入queue里面, 因为是BFS, 所以每一层最靠近0的位置都会被替换, 并且标记为visited, 然后一直BFS到最后即可.
时间复杂度降为O(m*n) 1. Constraints
1) empty or len(rooms[0]) == 0, edge case
2) size of the rooms < inf
3) 每个元素的值为0, -1, inf 2. Ideas BFS T: O(m*n) S: O(m*n) 1) edge case,empty or len(rooms[0]) == 0 => return
2) scan 2D arr, 将值为0 的append进入queue里面
3) BFS, 如果是not visited过得, 并且 != 0 or -1, 更改值为dis + 1, 另外append进入queue, add进入visited 3. Code
class Solution:
def wallAndGates(self, rooms):
if not rooms or len(rooms[0]) == 0: return
queue, lr, lc, visited, dirs = collections.deque(), len(rooms), len(rooms[0]), set(), [(1, 0), (-1, 0), (0, 1), (0, -1)]
for i in range(lr):
for j in range(lc):
if rooms[i][j] == 0:
queue.append((i, j, 0))
visited.add((i, j)) while queue:
pr, pc, dis = queue.popleft()
for d1, d2 in dirs:
nr, nc = pr + d1, pc + d2
if 0<= nr < lr and 0<= nc < lc and (nr, nc) not in visited and rooms[nr][nc] != -1:
rooms[nr][nc] = dis + 1
queue.append((nr,nc, dis + 1))
visited.add((nr, nc))
4. Test cases
1) edge case
2)
INF -1 0 INF
INF INF INF -1
INF -1 INF -1
0 -1 INF INF
After running function, the 2D grid should be:
3 -1 0 1
2 2 1 -1
1 -1 2 -1
0 -1 3 4
[LeetCode] 286. Walls and Gates_Medium tag: BFS的更多相关文章
- [LeetCode] 286. Walls and Gates 墙和门
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- [LeetCode] 127. Word Ladder _Medium tag: BFS
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- LeetCode 286. Walls and Gates
原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...
- [LeetCode] 101. Symmetric Tree_ Easy tag: BFS
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [LeetCode] 133. Clone Graph_ Medium tag: BFS, DFS
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- [LeetCode] 310. Minimum Height Trees_Medium tag: BFS
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- [LeetCode] 301. Remove Invalid Parentheses_Hard tag:BFS
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- [LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS
In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...
- [LeetCode] 821. Shortest Distance to a Character_Easy tag: BFS
Given a string S and a character C, return an array of integers representing the shortest distance f ...
随机推荐
- VIM 如何使用系统的剪切板
想要将系统剪贴板里的内容复制到 vi 编辑的文档中怎么办? 例如,在网页上复制了一段文字,想贴到本地的某个文件中. 使用 vi 打开本地文件,在 输入 模式下,按 Shift + Insert 详细可 ...
- 转载:C/C++关于string.h头文件和string类
学习C语言时,用字符串的函数例如stpcpy().strcat().strcmp()等,要包含头文件string.h 学习C++后,C++有字符串的标准类string,string类也有很多方法,用s ...
- 对Array进行排序(按字母顺序)
通过使用java.utils.Arrays.sort()和String.CASE_INSENSITIVE_OREDR,可以很容易的对结果进行排序(按字母顺序): String[] array = { ...
- Android 全屏Activity以透明的对话框形式弹出
1. styles.xml <style name="transcutestyle" parent="@android:style/Theme.DeviceDefa ...
- 面试题:应用中很多jar包,比如spring、mybatis、redis等等,各自用的日志系统各异,怎么用slf4j统一输出?(上)
一.问题概述 如题所说,后端应用(非spring boot项目)通常用到了很多jar包,比如spring系列.mybatis.hibernate.各类连接数据库的客户端的jar包.可能这个jar包用的 ...
- nginx fastcgi配置
1.1 nginx概述nginx简介Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/P ...
- Apache 的mod_auth_cas模块的介绍和使用
apache的mod_auth_cas模块是一个集成到apache中的cas客户端,一般是配合Apache的反向代理来使用,对某个url的请求先经过apache,apache会判断是否经过cas认证, ...
- 使用Btrace来动态监控方法的参数和返回值
btrace简介: btrace是一种动态跟踪分析一个运行中的Java应用程序的工具,它基于java的探针技术动态地向目标应用程序的字节码注入追踪代码(字节码追踪),这些追踪字节码追踪代码使用Java ...
- INTRO: THE DAWN (亡灵序曲) 中独白
As the last ship sailed towards the distant horizon I sat there watching on a rock My mind slowly dr ...
- php---进行签名验证
为什么要进行签名,我们在进行数据请求的时候,为了防止数据被人截取,造成不好的影响,所以我们在进行数据请求的时候,需要进行签名验证,进行签名的原理是:客户端和服务端使用同样的签名算法,来计算签名,当客户 ...