LeetCode 849 Maximize Distance to Closest Person (Python)

Posted by 小明MaxMing on October 29, 2020

题目

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.

解题思路

数最多连续0的个数(n),那么答案为(n + 1) / 2,单独处理两端

代码

class Solution:
    def maxDistToClosest(self, seats: List[int]) -> int:
        cur, n = 0, len(seats)
        for k in range(n):
            if seats[k]:
                break
            else:
                cur += 1
        res = cur
        zeros = cur = 0
        for i in range(k, n):
            if seats[i]:
                zeros = max(zeros, cur)
                cur = 0
            else:
                cur += 1
        res = max(res, (zeros + 1) // 2, cur)
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅