You are given an array representing a row of
seats
whereseats[i] = 1
represents a person sitting in theith
seat, andseats[i] = 0
represents that theith
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.
class Solution:
def maxDistToClosest(self, seats):
zeros = 0
pre_zero, max_zero, suf_zero = -1, -1, -1
for i in seats:
if i == 0:
zeros += 1
else:
# 更新 pre_zero
if pre_zero == -1:
pre_zero = zeros
# 更新 max_mid_zero
else:
max_zero = max(max_zero, zeros)
zeros = 0
# 更新 suf_zero
suf_zero = zeros
return max(pre_zero, suf_zero, (max_zero+1)//2)