#1552

Magnetic Force Between Two Balls

pupil · 540 · lc medium +28 · verified · 71.9% accepted · 3,182 likes · top 81%

Description

Given an array position representing basket locations and an integer m representing the number of balls, place all m balls into distinct baskets so that the minimum distance between any two balls is as large as possible. Return that maximum minimum distance.

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.

Code

1
2
3