一.题目:
给出了一个列表表示一排座位,1代表这个位置有人坐,0代表这个位置没人做。现在需要找一个位置坐,并找出坐在哪个位置时,离旁边人的座位的距离最大,是多少。
二.解题思路:
我们可以先将座位列表分割开,记录每块部分的大小,取其中最大的一块.同时注意,座位两端的位置要分开考虑.代码如下:
class Solution(object):
def maxDistToClosest(self, seats):
"""
:type seats: List[int]
:rtype: int
"""
new_seats = seats[:]
for i in range(len(new_seats)):
new_seats[i] = str(new_seats[i])
str_seats = "".join(new_seats)
zero_list = str_seats.split("1")
max_lens = 0
for zero in zero_list:
max_lens = max(max_lens,len(zero))
max_lens = max(1,(max_lens+1)/2)
cnt = 0
if seats[0] != 1:
for j in seats:
if j != 1:
cnt +=1
else:
break
max_lens = max(max_lens,cnt)
cnt = 0
if seats[-1] != 1:
for j in seats[::-1]:
if j != 1:
cnt +=1
else:
break
max_lens = max(max_lens,cnt)
return max_lens