849. Maximize Distance to Closest Person

In a row of seats1 represents a person sitting in that seat, and 0 represents that the seat is empty. 

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 closest person.

给一个数组,要么0要么1,至少会有1个0和1个1,1表示有人坐了,0表示没有人坐。

求找到一个位置,是的离最近的人的距离最大。

问题就是转换成找距离最远的两个1端点,然后答案是两个1的距离除以2,当然边界条件是只有1个1,这个时候就不需要除以2

class Solution(object):
    def maxDistToClosest(self, seats):
        """
        :type seats: List[int]
        :rtype: int
        """
        index = 0
        while index < len(seats) and seats[index] != 1:
            index += 1
        ans = index
        last = index
        for i in range(index + 1, len(seats), 1):
            if seats[i] == 1:
                ans = max(ans, (i - last) // 2)
                last = i
        ans = max(ans, (len(seats) - last - 1))
        return ans
原文地址:https://www.cnblogs.com/whatyouthink/p/13301966.html