Leetcode 1675 Solution
This article provides solution to leetcode question 1675 (magnetic-force-between-two-balls)
Access this page by simply typing in "lcs 1675" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/magnetic-force-between-two-balls
Solution
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
position.sort()
def isfit(force):
nonlocal position
nonlocal m
last_ball_pos = position[0]
c = 1
for i in range(1, len(position)):
if position[i] < last_ball_pos + force:
continue
last_ball_pos = position[i]
c += 1
return c >= m
l = 1
r = position[-1] - position[0]
while l < r:
force = (l + r + 1) // 2
if not isfit(force):
r = force - 1
else:
l = force
return l