You are given a m x n 2D grid initialized with these three possible values.

  1. -1 - A wall or an obstacle.
  2. 0 - A gate.
  3. INF - Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647.

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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. LeetCode 286. Walls and Gates

    原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. java高级---->Thread之单例模式的使用

    这里我们介绍一下在多线程中如何安全正确的编写单例模式的代码.不知为何,恰如其分的话总是姗姗来迟,错过最恰当的时机. 多线程中的单例模式 这里面通过代码来体会一下在多线程中如何正确的编写单例模式的代码. ...

  2. 如何使用 Flexbox 和 CSS Grid,实现高效布局

    CSS 浮动属性一直是网站上排列元素的主要方法之一,但是当实现复杂布局时,这种方法不总是那么理想.幸运的是,在现代网页设计时代,使用 Flexbox 和 CSS Grid 来对齐元素,变得相对容易起来 ...

  3. jQuery事件处理(一)

    1.jQuery事件绑定的用法: $( "elem" ).on( events, [selector], [data], handler ); events:事件名称,可以是自定义 ...

  4. java(3) 面向对象

    1.super关键字 * 使用super关键字调用父类的成员变量和成员方法.具体格式: super.成员变量 super.成员方法([参数1,参数2...]) * 使用super关键字调用父类的构造方 ...

  5. sendfile Linux函数

    现在流行的 web 服务器里面都提供sendfile 选项用来提高服务器性能,那到底 sendfile 是什么,怎么影响性能的呢? sendfile 实际上是 Linux 2.0+ 以后的推出的一个系 ...

  6. Elasticsearch学习之深入搜索六 --- 平衡搜索结果的精准率和召回率

    1. 召回率和精准度 比如你搜索一个java spark,总共有100个doc,能返回多少个doc作为结果,就是召回率,recall 精准度,比如你搜索一个java spark,能不能尽可能让包含ja ...

  7. openvpn 多机房互联

    Server端安装配置 一.安装 1.下载及相关依赖包安装 wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.06.tar.gz ...

  8. svn版本管理

    代码发布方案: 1,安装,优化 软件环境,(nginx,lvs)  <-------运维工程师 2,程序代码(不断更新).   <--------开发工程师,(开发,运维都可以发布) 3, ...

  9. 【CF757G】Can Bash Save the Day? 可持久化点分树

    [CF757G]Can Bash Save the Day? 题意:给你一棵n个点的树和一个排列${p_i}$,边有边权.有q个操作: 1 l r x:询问$\sum\limits_{i=l}^r d ...

  10. Spark2 生存分析Survival regression

    在spark.ml中,实现了加速失效时间(AFT)模型,这是一个用于检查数据的参数生存回归模型. 它描述了生存时间对数的模型,因此它通常被称为生存分析的对数线性模型. 不同于为相同目的设计的比例风险模 ...