【leetcode】1162. As Far from Land as Possible
题目如下:
Given an N x N
gridcontaining only values0and1, where0represents water and1represents land, find a water cell such that its distance to the nearest land cell is maximized and return the distance.The distance used in this problem is the Manhattan distance: the distance between two cells
(x0, y0)and(x1, y1)is|x0 - x1| + |y0 - y1|.If no land or water exists in the grid, return
-1.Example 1:
Input: [[1,0,1],[0,0,0],[1,0,1]]
Output: 2
Explanation:
The cell (1, 1) is as far as possible from all the land with distance 2.Example 2:
Input: [[1,0,0],[0,0,0],[0,0,0]]
Output: 4
Explanation:
The cell (2, 2) is as far as possible from all the land with distance 4.Note:
1 <= grid.length == grid[0].length <= 100grid[i][j]is0or1
解题思路:题目不难,注意用BFS,如果用DFS会超时。
代码如下:
class Solution(object):
def maxDistance(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
val = [[float('inf')] * len(grid[0]) for _ in grid]
queue = [] for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 1:
queue.append((i, j, 0)) direction = [(0, 1), (0, -1), (1, 0), (-1, 0)]
while len(queue) > 0:
x, y, dis = queue.pop(0)
for (x1, y1) in direction:
if x + x1 >= 0 and x + x1 < len(grid) and y + y1 >= 0 \
and y + y1 < len(grid[0]) and grid[x + x1][y + y1] == 0 \
and val[x + x1][y + y1] > dis + 1:
val[x + x1][y + y1] = dis + 1
queue.append((x + x1, y + y1, dis + 1)) res = -1
for i in range(len(val)):
for j in range(len(val[i])):
if val[i][j] != float('inf') and res < val[i][j]:
res = val[i][j] return res
【leetcode】1162. As Far from Land as Possible的更多相关文章
- 【LeetCode】1162. 地图分析 As Far from Land as Possible(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 这个题想考察什么? 剩下的任务就是套模板! 日期 题目 ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【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 ...
随机推荐
- 中国MOOC_零基础学Java语言_期末考试的编程题_1二进制的前导的零
期末考试的编程题 返回 这是期末考试的编程题,在60分钟内,你可以多次提交,直到正确为止. 温馨提示: 1.本次考试属于Online Judge题目,提交后由系统即时判分. 2.学生可以在考试截止 ...
- 使用cython库对python代码进行动态编译达到加速效果及python第三方包的制作安装
1.测试代码:新建 fib.pyx # coding:utf-8 import matplotlib.pyplot as plt import numpy as np from sklearn.cl ...
- springcloud(一)
Spring Cloud是什么鬼? Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线. ...
- Java第四周总结+实验报告
实验二 Java简单类与对象 实验目的 掌握类的定义,熟悉属性.构造函数.方法的作用,掌握用类作为类型声明变量和方法返回值: 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性 ...
- Python--笔试题
一.如何提高Python的运行效率: 1.使用生成器,因为可以节约大量内存 2.循环代码优化,避免过多重复代码的执行 3.核心模块用Cython PyPy等,提高效率 4.多进程.多线程.协程 5.多 ...
- spring - 第N篇 一些笔记
1.properties文件的引入 <bean id="propertyConfigurer" class="org.springframework.beans.f ...
- PHP7中的数据类型(一)计数引用、写时复制,可垃圾回收
列个简单的表格说明一下:
- 前端导出excel文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- webpack e6转化成es5 配置方法
方法一: https://www.babeljs.cn/setup#installation 按照babel官方的配置配 方法二: https://www.jianshu.com/p/ce28cedd ...
- [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)
[BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...