[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 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 <= seats.length <= 20000seatscontains only 0s or 1s, at least one0, and at least one1.
思路就是类似于[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的更多相关文章
- [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 ...
- 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 ...
- 849. Maximize Distance to Closest Person ——weekly contest 87
849. Maximize Distance to Closest Person 题目链接:https://leetcode.com/problems/maximize-distance-to-clo ...
- 【Leetcode_easy】849. Maximize Distance to Closest Person
problem 849. Maximize Distance to Closest Person solution1: class Solution { public: int maxDistToCl ...
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- 849. Maximize Distance to Closest Person
class Solution { public: int maxDistToClosest(vector<int>& seats) { ; ; for(int i:seats) / ...
- [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 ...
- C#LeetCode刷题之#849-到最近的人的最大距离(Maximize Distance to Closest Person)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3754 访问. 在一排座位( seats)中,1 代表有人坐在座位 ...
随机推荐
- STL之map学习实例
``` #include<iostream> #include<algorithm> #include<vector> #include<map> #i ...
- PAT甲级1060 Are They Equal【模拟】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805413520719872 题意: 给定两个数,表示成0.xxxx ...
- 阿里云不同账号之间相同地域的VPC网络互访
今天实际操作了一下,在这篇随笔中记录一下以备忘,主要参考阿里云帮助文档-不同账号下专有网络内网互通. 实现场景:账号A的VPC网络中的ECS访问账号B的VPC网络中的ECS与RDS(地域都在华东1), ...
- BZOJ 1003 - 物流运输 - [最短路+dp]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1003 Time Limit: 10 Sec Memory Limit: 162 MB D ...
- POJ 1456 - Supermarket - [贪心+小顶堆]
题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarke ...
- 2015年蓝桥杯省赛A组c++第3题
/* 小明发现了一个奇妙的数字.它的平方和立方正好把0~9的10个数字每个用且只用了一次. 你能猜出这个数字是多少吗? 请填写该数字,不要填写任何多余的内容. */ #include<cstdi ...
- zabbix zatree centos7安装zabbix-agent
https://github.com/Emersonxuelinux/zatree-3.0-/tree/master/zabbix-3.0.x /bin/sh /config/ds.sh /tmp/z ...
- 打开对话框opendialog
//使用:OpenDialog控件. ldg.Options := ldg.Options + [ofAllowMultiSelect]; ldg.Filter := '作答文件ZF包.zf| ...
- bug: '\xff' 转换成-1 而不是255
后台给的值处理后 Byte rtncode = payload[0]; 打印payload[0]是'\xff', 增加 if (rtncode ==255 ){ ....} 的判断,跳里面去了 然后用 ...
- zabbix准备:php安装
一.安装php依赖库 ftp://xmlsoft.org/libxml2/libxml2-2.9.3.tar.gz yum install python-devel -y cd /download/ ...