Skip to content

Commit aaef006

Browse files
committed
Maximize Distance to Closest Person
1 parent 4f0eea2 commit aaef006

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is empty.
2+
#
3+
# There is at least one empty seat, and at least one person sitting.
4+
#
5+
# Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.
6+
#
7+
# Return that maximum distance to closest person.
8+
#
9+
# Example 1:
10+
#
11+
# Input: [1,0,0,0,1,0,1]
12+
# Output: 2
13+
# Explanation:
14+
# If Alex sits in the second open seat (seats[2]), then the closest person has distance 2.
15+
# If Alex sits in any other open seat, the closest person has distance 1.
16+
# Thus, the maximum distance to the closest person is 2.
17+
# Example 2:
18+
#
19+
# Input: [1,0,0,0]
20+
# Output: 3
21+
# Explanation:
22+
# If Alex sits in the last seat, the closest person is 3 seats away.
23+
# This is the maximum distance possible, so the answer is 3.
24+
25+
26+
class Solution:
27+
def maxDistToClosest(self, seats):
28+
dist = 0
29+
while dist < len(seats) and seats[dist] == 0:
30+
dist += 1
31+
zero = 0
32+
33+
for i in range(dist + 1, len(seats)):
34+
if seats[i] == 0:
35+
zero += 1
36+
else:
37+
dist = max(dist, (zero + 1) // 2)
38+
zero = 0
39+
40+
return max(dist, zero)

0 commit comments

Comments
 (0)