1552. Magnetic Force Between Two Balls

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1552

class Solution:
    def maxDistance(self, position: List[int], m: int) -> int:
        def check(f):
            prev = position[0]
            cnt = 1
            for curr in position[1:]:
                if curr - prev >= f:
                    prev = curr
                    cnt += 1
            return cnt >= m

        position.sort()
        left, right = 1, position[-1]
        while left < right:
            mid = (left + right + 1) >> 1

            if check(mid):
                left = mid
            else:
                right = mid - 1
        return left