[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 = 2147483647
to representINF
as 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 ...
随机推荐
- Python pyQt4/PyQt5 学习笔记4(事件和信号)
信号 & 槽 import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets import (QWidget,QLCDNumber,QS ...
- sencha touch 压缩js,css遇到的问题
在使用工具压缩css和jss时,我遇到了以下问题 1. showBtn: { tap: function (t, value) { this.redirectTo(t.config.goto); } ...
- [原]secureCRT 改变显示宽度
1.首先全局设置:Options - Global Options - Terminal - Appearance - Maximumcolumns 最大只能设置成1024(推荐256),设置越大越占 ...
- 阻止form表单提交的问题
阻止form表单提交这种场景可能在生活中,我们经常碰到,而在我们第一印象里面可能我们用return false 去阻止表单默认行为. 但是,有中情况我们用return false 不能阻止表单提交 & ...
- [LintCode] Find the Missing Number 寻找丢失的数字
Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array. Example G ...
- linux 系统信息查看
查看系统版本:lsb_release -a 查看内核版本:uname -a 查看cpu型号:cat /proc/cpuinfo 查看硬盘空间情况df -lm 查看内存:free -m VGA显卡:l ...
- Keepalived指定文件接收日志
keepalived默认日志接收文件为/var/log/messages不方便查看,可以指定文件接收日志 修改配置文件/etc/sysconfig/keepalived KEEPALIVED_OPTI ...
- Hive之import和export使用详解
在hive-0.8.0后引入了import/export命令. Export命令可以导出一张表或分区的数据和元数据信息到一个输出位置,并且导出数据可以被移动到另一个hadoop集群或hive实例,并且 ...
- iOS + Node + MySQL
最近有空,又温习了一下Node ,配合Express 4.x可以很快的搭建一个简单的后台. Node比较适合频繁I/O,大量异步.至于更加复杂的后台逻辑还是用Java,个中滋味自己体验. Expres ...
- TP5.0 controller下分版本 分块 分目录写法访问
这个写法是在api模块下的controller目录下又分出了v1和以后未知的v(x)版本,这个时候需要配置路由来访问这个接口类似于这种形式: http://www.xx.com/index.php/a ...