Leetcode849.Maximize Distance to Closest Person到最近的人的最大距离
在一排座位( 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 <= seats.length <= 20000
- 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到最近的人的最大距离的更多相关文章
- [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 ...
- 【Leetcode_easy】849. Maximize Distance to Closest Person
problem 849. Maximize Distance to Closest Person solution1: class Solution { public: int maxDistToCl ...
- 849. Maximize Distance to Closest Person ——weekly contest 87
849. Maximize Distance to Closest Person 题目链接:https://leetcode.com/problems/maximize-distance-to-clo ...
- [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 ...
- [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 ...
- 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 ...
- C#LeetCode刷题之#849-到最近的人的最大距离(Maximize Distance to Closest Person)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3754 访问. 在一排座位( seats)中,1 代表有人坐在座位 ...
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Cmp- Linux必学的60个命令
1.作用 cmp(“compare”的缩写)命令用来简要指出两个文件是否存在差异,它的使用权限是所有用户. 2.格式 cmp[options] 文件名 3.[options]主要参数 -l: 将字节以 ...
- HZOI20190803 A,C题
题目链接:https://www.cnblogs.com/Juve/articles/11295333.html A: 考场上只有70分... 发现几个性质:特殊性质1:在两条链上,看它是fib第几项 ...
- RuntimeError: You called this URL via POST, but the URL doesn’t end in a slash and you have APPEND_SLASH set.
做公众号测试的时候,发现了个问题: 提交表单报错:RuntimeError: You called this URL via POST, but the URL doesn’t end in a sl ...
- TFS2013 微软源代码管理工具 安装与使用图文教程
最近公司新开发一个项目要用微软的TFS2013进行项目的源代码管理,以前只是用过SVN,从来没有用过TFS,所以在网上百度.谷歌了好一阵子来查看怎么安装和配置,还好花了一天时间总算是初步的搞定了,下面 ...
- R语言之数据处理
R语言之数据处理 一.向量处理 1.选择和显示向量 data[1] data[3] data[1:3] data[-1]:除第一项以外的所有项 data[c(1,3,4,6)] data[data&g ...
- VS C++/ClI调用C++ 外部Dll无法查看变量值
C#项目调用C++/ClI项目,C++/ClI项目又引用了外部C++ dll时 C++/CLI代码中在调试时无法查看native 变量的值 解决方法:C#项目右键属性-->Debug--> ...
- JavaScript怎么解析后台传入的json字符串
var data = "{'name': '张三', 'age': 23, 'gender': true}"; //json字符串 var jso = JSON.parse(dat ...
- Http请求中的Content-Type
转载至:https://segmentfault.com/a/1190000013056786?utm_source=tag-newest 一 前言 ----现在搞前端的不学好http有关的知识已经不 ...
- 用py3的nonlocal来打破局部变量间的作用域
nonlocal:用于局部变量,找上层中离当前函数最近一层的局部变量,找到为止,如果在全局找到或找不到,报错. 使用场景:内层函数对外层数据修改/处理
- Vue. 之 Element dialog 拖拽
Vue. 之 Element dialog 拖拽 默认情况下,在使用Element的Dialog模块时,弹出框是不能移动的,且 一旦点击遮罩层区域,弹框就会消失. 解决方案: 1 在 utils 中新 ...