849. Maximize Distance to Closest Person
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 49% Topics: Array
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
class Solution(object):
def maxDistToClosest(self, seats):
"""
:type seats: List[int]
:rtype: int
"""
prev, result = -1, 1
for i in xrange(len(seats)):
if seats[i]:
if prev < 0:
result = i
else:
result = max(result, (i-prev)//2)
prev = i
return max(result, len(seats)-1-prev)
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions