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. python3安装PIL

    原创    2017-09-29 16:15:27 系统环境: 64位win10系统,同时安装python2.7与python3.6两个版本 安装: PIL是Python平台事实上的图像处理标准库,支 ...

  2. day8 八、文件操作模式、文件的复制与文件游标操作

    一.文件操作 1.wr模式结合 ① w = open('1.txt', 'w', encoding='utf-8') # w:没有文件新建文件,有文件就清空文件 w.write('000\n') w. ...

  3. Linux 创建文件系统及挂载文件系统流程详解(转)

    作者:北南南北 来自: LinuxSir.Org 摘要:本文对新增硬盘,切割硬盘,创建硬盘分区,为硬盘分区创建文件系统,以及加载文件系统的流程做总结性论述:主要是为初学者弄清楚这一操作过程:本文涉及f ...

  4. scokte tcp/ip

    import scoket# 服务端 server = socket.socket() ip_port = ("127.0.0.1",8001) server.bind(ip_po ...

  5. POJ 2299 Ultra-QuickSort 离散化加树状数组求逆序对

    http://poj.org/problem?id=2299 题意:求逆序对 题解:用树状数组.每读入一个数x,另a[x]=1.那么a数列的前缀和s[x]即为x前面(或者说,再x之前读入)小于x的个数 ...

  6. spark application提交应用的两种方式

    bin/spark-submit --help ... ... --deploy-mode DEPLOY_MODE   Whether to launch the driver program loc ...

  7. hive优化之开启压缩功能

    1.开启hive作业mapreduce任务中间压缩功能: 对于数据进行压缩可以减少job中map和reduce task间的数据传输量.对于中间数据压缩,选择一个低cpu开销编/解码器要不选择一个压缩 ...

  8. 解决eclipse在线安装不了genymotion

    解决eclipse在线安装不了genymotion 2015-8-2阅读161 评论0 今天在网上看见网友说genymotion安卓模拟器是多麽的流畅,比起google自带的那些emulator快十倍 ...

  9. manifold 微分流形上可以定义可微函数、切向量、切向量场、各种张量场等对象并建立其上的分析学,并可以赋予更复杂的几何结构以研究它们的性质。

    小结: 1.流形(英语:Manifolds)一般可以通过把许多平直的片折弯并粘连而成,是局部具有欧几里得空间性质的空间,是欧几里得空间中的曲线.曲面等概念的推广 2.描述一个流形往往需要不止一个“地图 ...

  10. php之print_r

    stdClass类是PHP的一个内部保留类,初始时没有成员变量也没有成员方法,所有的魔术方法都被 设置为null,可以使用其传递变量参数,但是没有可以调用的方法.stdClass类可以被继承. 只是这 ...