In a row of seats1 represents a person sitting in that seat, and 0 represents that the seat is empty.

There is at least one empty seat, and at least one person sitting.

Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.

Return that maximum distance to closest person.

Example 1:

Input: [1,0,0,0,1,0,1]
Output: 2
Explanation:
If Alex sits in the second open seat (seats[2]), then the closest person has distance 2.
If Alex sits in any other open seat, the closest person has distance 1.
Thus, the maximum distance to the closest person is 2.

Example 2:

Input: [1,0,0,0]
Output: 3
Explanation:
If Alex sits in the last seat, the closest person is 3 seats away.
This is the maximum distance possible, so the answer is 3.

Note:

  1. 1 <= seats.length <= 20000
  2. seats contains only 0s or 1s, at least one 0, and at least one 1.

思路就是类似于[LeetCode] 286. Walls and Gates_Medium tag: BFS

Code: T: O(n) S; O(n)

class Solution:
def maxDistToClosest(self, seats):
"""
:type seats: List[int]
:rtype: int
"""
queue, ans, visited, lr = collections.deque(), 0, set(), len(seats)
for i in range(lr):
if seats[i]:
queue.append((i,0))
visited.add(i) while queue:
node, dis = queue.popleft()
for c in [-1,1]:
nr = node + c
if 0 <= nr < lr and nr not in visited and not seats[nr]:
queue.append((nr, dis+1))
visited.add(nr)
ans = max(ans, dis+1)
return ans

[LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS的更多相关文章

  1. [LeetCode] 849. Maximize Distance to Closest Person 最大化最近人的距离

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  2. leetcode 849. Maximize Distance to Closest Person

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  3. 849. Maximize Distance to Closest Person ——weekly contest 87

    849. Maximize Distance to Closest Person 题目链接:https://leetcode.com/problems/maximize-distance-to-clo ...

  4. 【Leetcode_easy】849. Maximize Distance to Closest Person

    problem 849. Maximize Distance to Closest Person solution1: class Solution { public: int maxDistToCl ...

  5. 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

  7. 849. Maximize Distance to Closest Person

    class Solution { public: int maxDistToClosest(vector<int>& seats) { ; ; for(int i:seats) / ...

  8. [LeetCode] Maximize Distance to Closest Person 离最近的人的最大距离

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  9. C#LeetCode刷题之#849-到最近的人的最大距离(Maximize Distance to Closest Person)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3754 访问. 在一排座位( seats)中,1 代表有人坐在座位 ...

随机推荐

  1. 查找C++代码中某一范围内的内存泄露

    #include <string.h> #define _CRTDBG_MAP_ALLOC #include <crtdbg.h> int _tmain(int argc, _ ...

  2. easyui datagrid 弹出页面会出现两个上下滚动条处理办法!

    同事推荐将datagrid上加一个toolbar 将上面的工具元素加上就可以了 toolbar: '#divListToolbar',

  3. RPC框架-通俗易懂的解释

    早期单机时代,一台电脑上运行多个进程,大家各干各的,老死不相往来.假如A进程需要一个画图的功能,B进程也需要一个画图的功能,程序员就必须为两个进程都写一个画图的功能.这不是整人么?于是就出现了IPC( ...

  4. 架构师如何借鉴他人经验快速成长? | 2018GIAC上海站日程上线!

    随着网络技术的迅猛发展,越来越多的企业需要紧跟技术发展潮流以应对层出不穷的业务场景变化.如今多“语言”开发百花齐放,选择何种语言才能在合适的场景中发挥最大价值?互联网业务架构经过了长年的发展,已然朝着 ...

  5. win10屏幕变灰怎么解决?

    朋友们在使用电脑过程中最高频使用的快捷键可能就是ctrl+c, ctrl+v了,但是殊不知,有时候按的太快产生误触,触发了ctrl+win+c,是屏幕变成了灰色,只需要再次同时按下这三个键就可以恢复彩 ...

  6. 阿里云mysql远程登录报ERROR 2027(HY000)

    mysql远程登录的命令是: mysql -h数据库地址 -u用户名 -p 但是用这个命令在登录阿里云的mysql时,会报ERROR 2027 (HY000): Malformed packet

  7. arcengine右键实现new group layer的功能

    没有找到相关方法,但是有对图层组进行操作的资料. https://gis.stackexchange.com/questions/43620/how-do-i-reach-a-layer-inside ...

  8. day-2 jmeter 操作mysql数据库

    1)  导入jdbc的jar包,因为jmeter本身不能直接连接mysql,所以需要导入第三方的jar包,来连接mysql 2)       创建数据库连接配置,mysql的url.端口号.账号.密码 ...

  9. pyqt5-day1

    pyqt5做为Python的一个模块,它有620多个类和6000个函数和方法.这是一个跨平台的工具包,它可以运行在所有主要的操作系统,包括UNIX,Windows,Mac OS.pyqt5是双重许可. ...

  10. VB改写C#

    1.VB的Val()函数 先从程序集中引入Microsoft.VisualBasic命名空间.不过,即便是引入了Microsoft.VisualBasic命名空间,还是不能直接使用像Val()这样的函 ...