当前位置:网站首页>leetcode-849:到最近的人的最大距离
leetcode-849:到最近的人的最大距离
2022-07-02 23:55:00 【菊头蝙蝠】
题目
给你一个数组 seats 表示一排座位,其中 seats[i] = 1 代表有人坐在第 i 个座位上,seats[i] = 0 代表座位 i 上是空的(下标从 0 开始)。
至少有一个空座位,且至少有一人已经坐在座位上。
亚历克斯希望坐在一个能够使他与离他最近的人之间的距离达到最大化的座位上。
返回他到离他最近的人的最大距离。
示例 1:
输入:seats = [1,0,0,0,1,0,1]
输出:2
解释:
如果亚历克斯坐在第二个空位(seats[2])上,他到离他最近的人的距离为 2 。
如果亚历克斯坐在其它任何一个空位上,他到离他最近的人的距离为 1 。
因此,他到离他最近的人的最大距离是 2 。
示例 2:
输入:seats = [1,0,0,0]
输出:3
解释:
如果亚历克斯坐在最后一个座位上,他离最近的人有 3 个座位远。
这是可能的最大距离,所以答案是 3 。
示例 3:
输入:seats = [0,1]
输出:1
解题
方法一:一次遍历
总共有三种情况
- 1.计算左边第一个有人的位置到左边的第一个位置距离(亚历克斯可以坐到最左边)
- 2.计算最右边有人的位置到最后一个位置距离(亚历克斯可以坐到最右边)
- 3.计算两个有人位置间的距离(亚历克斯可以坐到两个人中间)
取这三种情况的最大值就行了。
class Solution {
public:
int maxDistToClosest(vector<int>& seats) {
int n=seats.size();
int first,pre,last;//记录最左边的有人的座位、上一个有人的座位、最后一个有人的座位
int maxInterval=INT_MIN;//记录两个座位之间最大间距
int count=0;
for(int i=0;i<n;i++){
if(seats[i]==1){
if(count==0){
//如果是第一个有人的座位
first=i;
last=i;
}else if(count>=1){
//如果是第一个以后 有人的座位
pre=last;
last=i;
maxInterval=max(maxInterval,last-pre);//保存当前有人的座位和上一个有人的座位 的 最大间距
}
count++;
}
}
return max({
first,n-1-last,maxInterval/2});//选择最大值
}
};
边栏推荐
- 1.12 - 指令
- 腾讯云免费SSL证书扩展文件含义
- Centos7 one click compilation to build MySQL script
- NC24840 [USACO 2009 Mar S]Look Up
- Introduction and use of ftrace tool
- 【AutoSAR 十二 模式管理】
- 关于QByteArray存储十六进制 与十六进制互转
- [shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
- Kubernetes simple introduction to writing YML
- node_modules删不掉
猜你喜欢

Rust string slicing, structs, and enumeration classes

Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t

pod生命周期详解
![[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)

文件操作IO-Part2

Illustrated network: what is virtual router redundancy protocol VRRP?

Shell 实现文件基本操作(sed-编辑、awk-匹配)

利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度

Solution to the problem of abnormal display of PDF exported Chinese documents of confluence

Why is the website slow to open?
随机推荐
Nc20806 District interval
Shell implements basic file operations (cutting, sorting, and de duplication)
MySQL 23 classic interview hanging interviewer
Some introduction and precautions about XML
Array common operation methods sorting (including ES6) and detailed use
Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)
机器学习:numpy版本线性回归预测波士顿房价
Form form instantiation
lex && yacc && bison && flex 配置的问题
Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
NC20806 区区区间间间
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
Use Jenkins II job
Leetcode 294. Flip game II (game theory)
【雅思阅读】王希伟阅读P2(阅读填空)
Basic use of shell script
Vulkan-性能及精细化
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function