849.Maximize Distance to Closest Person(题目地址链接:https://leetcode.com/problems/maximize-distance-to-c)

You are given an array representing a row of seats where seats[i] = 1 represents a person sitting in the ith seat, and seats[i] = 0 represents that the ith seat is empty (0-indexed).

There is at least one empty seat, and at least one person sitting.

Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.

Return that maximum distance to the closest person.

Example 1:

Input: seats = [1,0,0,0,1,0,1]

Output: 2

Explanation:

If Alex sits in the second open seat (i.e. seats[2]), then the closest person has distance 2.

If Alex sits in any other open seat, the closest person has distance 1.

Thus, the maximum distance to the closest person is 2.

Example 2:

Input: seats = [1,0,0,0]

Output: 3

Explanation:

If Alex sits in the last seat (i.e. seats[3]), the closest person is 3 seats away.

This is the maximum distance possible, so the answer is 3.

Example 3:

Input: seats = [0,1]

Output: 1

Constraints:

2 <= seats.length <= 2 * 104

seats[i] is 0 or 1.

At least one seat is empty.

At least one seat is occupied.

Accepted

131,321

Submissions

290,885

Seen this question in a real interview before?

翻译

849.与最近的人的最远距离

你被给出了一个代表一排座位的数组,座位seats[i]=1代表一个坐在第i个座位的人,并且seats[i]=0代表第i个座位是空的(0是被定义的)

至少有一个空的座位,并且至少有一个人在坐着。

Alex想要坐在座位上为了保证他和最近的人的距离是最大的。

请返回和最近的人之间的最大距离。

例1:

输入: seats={1,0,0,0,1,0,1}

输出: 2

注释:如果Alex坐在第二个没人的座位上(i,e,seats[2]),那么与最近的那个人的距离就是2。如果Alex坐在其他的没人的座位上,那么与最近的那个人的距离就是1.

因此,与最近的人的最大距离就为2.

例2:

输入:seats={1,0,0,0}

输出:3

注释:如果Alex坐在最后一个座位上(i,e.seats[3]),与最近的的人的距离就是3.

这是最大距离的可能性,

所以答案是3。

例3:

输入:seats={0,1}

输出:1

约束条件: 

·2<=seats的长度<=2*1e4

·seats[i] 是0或1

·至少有一个座位是空的

·至少有一个座位被占

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值