当前位置:网站首页>Leetcode-849: maximum distance to the nearest person
Leetcode-849: maximum distance to the nearest person
2022-07-03 00:47:00 【Chrysanthemum headed bat】
leetcode-849: The maximum distance to the nearest person
subject
Give you an array seats
Indicates a row of seats , among seats[i] = 1
It means someone is sitting on the i In one seat ,seats[i] = 0
Delegate seat i
The is empty ( Subscript from 0 Start ).
There is at least one empty seat , There is at least one person sitting in the seat .
Alex wants to be in a seat that maximizes the distance between him and the person closest to him .
Return to his maximum distance to the nearest person .
Example 1:
Input :seats = [1,0,0,0,1,0,1]
Output :2
explain :
If Alex is in the second seat (seats[2]) On , The distance from him to the nearest person is 2 .
If Alex is sitting in any other empty seat , The distance from him to the nearest person is 1 .
therefore , The biggest distance he can get to the nearest person is 2 .
Example 2:
Input :seats = [1,0,0,0]
Output :3
explain :
If Alex is in the last seat , He is the nearest person 3 Seats away .
This is the maximum distance possible , So the answer is 3 .
Example 3:
Input :seats = [0,1]
Output :1
Problem solving
Method 1 : One traverse
There are three situations
- 1. Calculate the distance from the first person on the left to the first person on the left ( Alex can sit on the far left )
- 2. Calculate the distance from the rightmost position with people to the last position ( Alex can sit on the far right )
- 3. Calculate the distance between two manned positions ( Alex can sit between two people )
Take the maximum of these three cases .
class Solution {
public:
int maxDistToClosest(vector<int>& seats) {
int n=seats.size();
int first,pre,last;// Record the left most seat 、 The last seat is occupied 、 The last seat is occupied
int maxInterval=INT_MIN;// Record the maximum distance between two seats
int count=0;
for(int i=0;i<n;i++){
if(seats[i]==1){
if(count==0){
// If it is the first seat with people
first=i;
last=i;
}else if(count>=1){
// If it is after the first Occupied seat
pre=last;
last=i;
maxInterval=max(maxInterval,last-pre);// Save the currently occupied seat and the last occupied seat Of Maximum spacing
}
count++;
}
}
return max({
first,n-1-last,maxInterval/2});// Select the maximum value
}
};
边栏推荐
- Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
- 【Pulsar文档】概念和架构/Concepts and Architecture
- 【AutoSAR 五 方法论】
- Graduation summary
- Introduction of UART, RS232, RS485, I2C and SPI
- Nc50528 sliding window
- Is there a free text to speech tool to help recommend?
- How to find out the currently running version of Solr- How do I find out version of currently running Solr?
- [golang syntax] map common errors golang panic: assignment to entry in nil map
- Form form instantiation
猜你喜欢
Sentry developer contribution Guide - configure pycharm
指针进阶(一)
【AutoSAR 八 OS】
1.12 - Instructions
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
[MCU project training] eight way answering machine
mm中的GAN模型架构
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
【AutoSAR 四 BSW概述】
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
随机推荐
node_ Modules cannot be deleted
Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
【AutoSAR 三 RTE概述】
Nc20806 District interval
Shell 实现文件基本操作(sed-编辑、awk-匹配)
Multi process programming (III): message queue
Multiprocess programming (I): basic concepts
Rust所有权(非常重要)
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
Andorid gets the system title bar height
NC24325 [USACO 2012 Mar S]Flowerpot
Multiprocess programming (4): shared memory
如何系统学习机器学习
The most painful programming problem in 2021, adventure of code 2021 Day24
[jetcache] jetcache configuration description and annotation attribute description
MySQL 23 classic interview hanging interviewer
How to systematically learn machine learning
【AutoSAR 十 IO架构】
Nc17059 queue Q
Vulkan-性能及精细化