【leetcode】1162. As Far from Land as Possible
题目如下:
Given an N x N
grid
containing only values0
and1
, where0
represents water and1
represents 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 <= 100
grid[i][j]
is0
or1
解题思路:题目不难,注意用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 ...
随机推荐
- 通过 vSphere WS API 获取 vCenter Datastore Provisioned Space 置备空间
目录 文章目录 目录 Provisioned Space & Used Space Provisioned Space 的计算方式 Uncommitted Space 扩展:置备率的计算公式 ...
- python检测进程是否存在
import win32com.client def check_exsit(process_name): WMI = win32com.client.GetObject('winmgmts:') p ...
- Centos6.5安装配置svn服务器
一. yum安装svn服务器 yum -y install subversion 二. 检测安装结果 svnserve --version //显示安装结果,表示安装成功了 三. 创建代码仓库目录 m ...
- CSS3—— 分页 框大小 弹性盒子 多媒体查询 多媒体查询实例
分页 网站有很多个页面,就需要使用分页来为每个页面做导航 点击及鼠标悬停分页样式 圆角样式 悬停过度效果 带边框的分页 分页间隔 分页字体大小 居中分页 面包屑导航 框大小 box-sizing 属性 ...
- HTML5——新表单元素 表单属性 语义元素
表单元素 新的表单元素 datalist——输入域选项列表 keygen——提供一种验证用户的可靠方法 output——不同类型的输出,比如计算或脚本输出 表单属性 <form> / &l ...
- 【SD系列】SAP SD凭证处理被批次处理冻结
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[SD系列]SAP SD凭证处理被批次处理冻结 ...
- Vue.js系列:生命周期钩子
开发人员提供了一个Web开发人员可以在Vue.js应用程序的整个生命周期中使用的各种方法的讨论. 生命周期钩子是在Vue对象生命周期的某个阶段执行的已定义方法.从初始化开始到它被破坏时,对象都会遵循不 ...
- HTTP 常见相应状态码及含义
1xx:信息 100 Continue 服务器仅接收到部分请求,但是一旦服务器并没有拒绝该请求,客户端应该继续发送其余的请求. 101 Switching Protocols 服务器转换协议:服务器将 ...
- ES6生成器与迭代器
ES6迭代器的一个例子 function run(taskDef) { var task = taskDef(); var result = task.next(); // 递归执行迭代 functi ...
- [BZOJ 3992] [SDOI 2015] 序列统计(DP+原根+NTT)
[BZOJ 3992] [SDOI 2015] 序列统计(DP+原根+NTT) 题面 小C有一个集合S,里面的元素都是小于质数M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数 ...