Skip to content

Commit 8384cd2

Browse files
Added solution of problem: Maximize Distance to Closest Person written in python language
1 parent b9cbd8d commit 8384cd2

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
def maxDistToClosest(self, arr: List[int]) -> int:
3+
4+
n = len(arr)
5+
6+
def closest_left(index):
7+
for i in range(index-1, -1, -1):
8+
if arr[i] == 1:
9+
return i
10+
return -1
11+
12+
def closest_right(index):
13+
for i in range(index + 1, n):
14+
if arr[i] == 1:
15+
return i
16+
return -1
17+
18+
pos = -float("inf")
19+
for i in range(n):
20+
if not arr[i]:
21+
left = closest_left(i)
22+
right = closest_right(i)
23+
d_right = float("inf")
24+
d_left = float("inf")
25+
if left != -1:
26+
d_left = i - left
27+
if right != -1:
28+
d_right = right - i
29+
30+
distance = min(d_left, d_right)
31+
pos = max(pos, distance)
32+
33+
return pos

0 commit comments

Comments
 (0)