1552. Magnetic Force Between Two Balls
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 71% Topics: Array, Binary Search, Sorting
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(nlogn + nlogr), r is the range of positions
# Space: O(1)
class Solution(object):
def maxDistance(self, position, m):
"""
:type position: List[int]
:type m: int
:rtype: int
"""
def check(position, m, x):
count, prev = 1, position[0]
for i in xrange(1, len(position)):
if position[i]-prev >= x:
count += 1
prev = position[i]
return count >= m
position.sort()
left, right = 1, position[-1]-position[0]
while left <= right:
mid = left + (right-left)//2
if not check(position, m, mid):
right = mid-1
else:
left = mid+1
return right
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions