In the universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n
empty baskets, the ith
basket is at position[i]
, Morty has m
balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x
and y
is |x - y|
.
Given the integer array position
and the integer m
. Return the required force.
Example 1:
Input: position = [1,2,3,4,7], m = 3 Output: 3 Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2 Output: 999999999 Explanation: We can use baskets 1 and 1000000000.
Constraints:
n == position.length
2 <= n <= 105
1 <= position[i] <= 109
position
are distinct.2 <= m <= position.length
This problem asks to find the maximum minimum magnetic force between any two balls when distributing m
balls into n
baskets located at positions given in the position
array. The magnetic force between two balls is the absolute difference of their positions.
The core idea is to use binary search. The problem exhibits monotonicity: a larger minimum force implies fewer balls can be placed. Therefore, we can efficiently search for the maximum minimum force using binary search.
Algorithm:
Sort: Sort the position
array in ascending order. This is crucial for efficiently checking if a given minimum force is achievable.
Binary Search: Perform a binary search on the range of possible minimum forces.
Check Feasibility (count function): In each iteration of the binary search, we need to check if a given minimum force (f
) is achievable. This is done using a helper function count(f)
.
count(f)
iterates through the sorted position
array.prev
).curr - prev
) is greater than or equal to f
. If it is, a ball can be placed at this position, incrementing the cnt
(number of balls placed).count(f)
returns the total number of balls that can be placed with a minimum force of f
.Binary Search Logic:
count(mid)
(where mid
is the midpoint in the binary search) is greater than or equal to m
, it means we can place m
balls with a minimum force of at least mid
. So, we update the lower bound (l
) to mid
.r
) to mid - 1
.Result: The final value of l
after the binary search represents the maximum minimum magnetic force achievable.
Time Complexity: O(n log n + n log M), where n is the number of baskets and M is the range of positions (approximately the maximum position value). The O(n log n) comes from sorting, and O(n log M) comes from the binary search and the count
function called in each iteration.
Space Complexity: O(log n) due to the recursive calls in the binary search (in some implementations). If the sorting algorithm uses in-place sorting, space complexity could be considered O(1)
Code Examples (Python):
import bisect
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
def check(f: int) -> bool:
prev = -float('inf')
cnt = 0
for curr in position:
if curr - prev >= f:
prev = curr
cnt += 1
return cnt >= m #changed to >= for correctness
position.sort()
l, r = 1, position[-1] - position[0] # improved upper bound
ans = 0
while l <= r:
mid = (l + r) // 2
if check(mid):
ans = mid
l = mid + 1
else:
r = mid - 1
return ans
This Python code directly implements the binary search. The check
function efficiently determines feasibility. The upper bound of the binary search is improved to position[-1] - position[0]
. This prevents unnecessary iterations.
Other languages (Java, C++, Go, TypeScript, JavaScript) follow a similar structure, adapting the syntax and data structures accordingly. The core logic of binary search and the feasibility check remains consistent.