Finish problem 1552-magnetic-force-between-two-balls
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
def balls_able(sorted_positions: list[int], min_distance) -> int:
|
||||
balls = 1
|
||||
anchor = sorted_positions[0]
|
||||
for position in sorted_positions[1:]:
|
||||
if position - anchor >= min_distance:
|
||||
balls += 1
|
||||
anchor = position
|
||||
|
||||
return balls
|
||||
|
||||
|
||||
class Solution:
|
||||
def maxDistance(self, position: list[int], m: int) -> int:
|
||||
sorted_positions = sorted(position)
|
||||
lo, hi = 1, max(position) - min(position)
|
||||
while lo < hi:
|
||||
limit = (lo + hi + 1) // 2
|
||||
if balls_able(sorted_positions, limit) >= m:
|
||||
lo = limit
|
||||
else:
|
||||
hi = limit - 1
|
||||
|
||||
return hi
|
||||
Reference in New Issue
Block a user