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
·至少有一个座位是空的
·至少有一个座位被占