-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm1552 Daily.py
35 lines (28 loc) · 1.01 KB
/
m1552 Daily.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
minn = maxx = position[0]
for p in position :
if p < minn :
minn = p
elif p > maxx :
maxx = p
left = 1 # 1 = worst case since m=len(positions) += 1 each indx worst
right = (maxx - minn) // (m - 1) # m - 1 = number of gaps so this is aideal case
while left < right :
mid = (left + right + 1) // 2
worked = False
counter = 1
prevVal = position[0]
for i in range(1, len(position)) :
if position[i] - prevVal >= mid :
counter += 1
prevVal = position[i]
if counter >= m :
worked = True
break
if worked :
left = mid
else :
right = mid - 1
return left