#849
Maximize Distance to Closest Person
specialist · 875 · lc medium +31 · verified · 49.6% accepted · 3,333 likes · top 37%
Description
You are given an array seats representing a row of seats, where seats[i] = 1 means a person is seated at position i and seats[i] = 0 means the seat is vacant.
At least one seat is empty and at least one is occupied.
Alex wants to sit in the empty seat that maximizes his distance to the nearest occupied seat.
Return that maximum distance.
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
Code
1
2
3