[220116] Maximize Distance to Closest Person

[220116] Maximize Distance to Closest Person,第1张

[220116] Maximize Distance to Closest Person

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.

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)

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5720845.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存