欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

[220116]MaximizeDistancetoClosestPerson

时间:2023-08-25

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)

 

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。