在一排座位( seats)中,1 代表有人坐在座位上,0 代表座位上是空的。

至少有一个空座位,且至少有一人坐在座位上。

亚历克斯希望坐在一个能够使他与离他最近的人之间的距离达到最大化的座位上。

返回他到离他最近的人的最大距离。

示例 1:

输入:[1,0,0,0,1,0,1] 输出:2 解释: 如果亚历克斯坐在第二个空位(seats[2])上,他到离他最近的人的距离为 2 。 如果亚历克斯坐在其它任何一个空位上,他到离他最近的人的距离为 1 。 因此,他到离他最近的人的最大距离是 2 。

示例 2:

输入:[1,0,0,0] 输出:3 解释: 如果亚历克斯坐在最后一个座位上,他离最近的人有 3 个座位远。 这是可能的最大距离,所以答案是 3 。

提示:

  1. 1 <= seats.length <= 20000
  2. seats 中只含有 0 和 1,至少有一个 0,且至少有一个 1。
class Solution {
public:
int maxDistToClosest(vector<int>& seats) {
int len = seats.size();
if(len <= 1)
return 0;
int MAX = 0;
if(seats[0] == 0)
{
int cnt = 0;
for(int i = 0; i < len; i++)
{
if(seats[i] == 0)
cnt++;
else
break;
}
MAX = max(MAX, cnt);
}
if(seats[len - 1] == 0)
{
int cnt = 0;
for(int i = len - 1; i >= 0; i--)
{
if(seats[i] == 0)
cnt++;
else
break;
}
MAX = max(MAX, cnt);
}
int cnt = 0;
for(int i = 1; i < len - 1; i++)
{
if(seats[i] == 0)
{
cnt++;
MAX = max((cnt + 1) / 2, MAX);
}
else
{
cnt = 0;
}
}
return MAX;
}
};

Leetcode849.Maximize Distance to Closest Person到最近的人的最大距离的更多相关文章

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

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

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

  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. [Swift]LeetCode849. 到最近的人的最大距离 | 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 ...

  5. [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 emp ...

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

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

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

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

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

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

随机推荐

  1. Nmap扫描原理(上)

    转自:https://blog.csdn.net/qq_34398519/article/details/89055991 Nmap是一款开源免费的网络发现(Network Discovery)和安全 ...

  2. Nginx报错汇总

    1.     Nginx 无法启动解决方法 在查看到 logs 中报了如下错误时: 0.0.0.0:80 failed (10013: An attempt was made to access a ...

  3. NtQuerySystemInformation 枚举进程

    函数原型: NTSTATUS WINAPI NtQuerySystemInformation(    _In_      SYSTEM_INFORMATION_CLASS SystemInformat ...

  4. List --搜索列表

    1,常见的内建函数 . if val in L :  # 是否在列表 . L.index(val) # 找下标 . L.count(val) # 有多少个 . min/max(L ) # 最大最小 . ...

  5. Redis高可用及集群

    目录 Redis主从复制 环境准备 主从复制命令 Redis Sentinel 功能 Redis Sentinel配置 Redis集群 Redis主从复制 使用异步复制 一个服务器可以有多个从服务器 ...

  6. Collection接口和Map接口

  7. 第五章 Odoo 12开发之导入、导出以及模块数据

    大多数Odoo 模块的定义,如用户界面和安全规则,实际是存储在对应数据表中的数据记录.模块中的 XML 和 CSV 文件不是 Odoo 应用运行时使用,而是载入数据表的手段.正是因为这个原因,Odoo ...

  8. 局部内部类为什么只能访问final局部变量,对于成员变量却可以随便访问?

    局部内部类为什么只能访问final局部变量,对于成员变量却可以随便访问? public class OuterClass { private int memberField = 10; public ...

  9. php函数基础(一)

    一.函数结构   1.构成部分:             关键字 function        

  10. [code]彩色图像直方图均衡化 histogram_rgb

    //2013.9 eageldiao #ifdef HISTOGRAM_RGB unsigned ]; unsigned intncount[]={},ncount1[]={},ncount2[]={ ...