在一排座位( 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. sql里面插入语句insert后面的values关键字可省略

    插入到表名(列值)后跟一个查询语句的话就代表值,简单的说就是后面select select出来的值就是要插入的值,即  insert into tb(字段名一,字段名二)select 字段名一,字段名 ...

  2. 09.Hibernate中的事务与并发

    事务1. 什么是事务 * 事务就是逻辑上的一组操作,组成事务的各个执行单元,操作要么全都成功,要么全都失败. * 转账的例子:冠希给美美转钱,扣钱,加钱.两个操作组成了一个事情! 2. 事务的特性 * ...

  3. PHP实现记录浏览历史页面

    <?php /******* 说明:cookie只能保存字符串 本实例中,需要保存多个URL(历史访问记录),思路是先将URL数组转为字符串,然后保存,读取时,再循环读取 *******/ // ...

  4. jeecms项目相关配置文件

    1.application-context.xml 这个是Spring的标准配置文件,这里面配置jdbc.properties文件并初始化相应数据库连接参数到bean实例:定义数据库表映射文件*.hb ...

  5. 《DSP using MATLAB》Problem 7.32

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  6. Python-Jsonpath简单入门

    原文来自:http://goessner.net/articles/JsonPath/   JSONPath - 是xpath在json的应用. xml最大的优点就有大量的工具可以分析,转换,和选择性 ...

  7. Python爬虫工程师必学——App数据抓取实战

    Python爬虫工程师必学 App数据抓取实战 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大 ...

  8. js 中直接调用和new的区别

    var test = new Test(); // 这里的 test 是什么?  是一个 Test 对象吗?错!这里 test 是一个函数——Test 中返回的 function() { return ...

  9. KeyOnlyFilter(2)

    主要用来过滤剩下行键计数一类 KeyOnlyFilter 官方API解释如下: A filter that will only return the key component of each KV ...

  10. 2018-8-14-Resharper-如何把类里的类移动到其他文件

    title author date CreateTime categories Resharper 如何把类里的类移动到其他文件 lindexi 2018-08-14 17:34:39 +0800 2 ...