Trains the technique from
LeetCode 1552Magnetic Force Between Two BallsThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A survey team works along one bank of a straight drainage channel where mooring posts have been driven into the ground. posts[i] is the distance in metres of one post from the channel mouth. The distances are all different and they arrive in no particular order.
The team has buoys instrument buoys to tie up. Each buoy must go on a post, and no post can take more than one buoy. Every buoy must be used. For an arrangement, the distance between two buoys is the absolute difference of their posts' distances, and the arrangement's clearance is the smallest distance between any two of the moored buoys.
Return the largest clearance that any arrangement can achieve.
Example 1
The reported figure is met by mooring on the posts at 2, 9 and 14, which leaves neighbouring buoys 7 and 5 metres apart.
Example 2
The reported figure is met by mooring at 1, 4 and 20, which leaves gaps of 3 and 16 metres.
Example 3
Only two posts exist and both must take a buoy, so the clearance is the span between them.
Example 4
Every post takes a buoy, so the clearance is the tightest gap along the whole line, which measures 4 metres.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def widest_spacing(posts: list[int], buoys: int) -> int:public int widestSpacing(int[] posts, int buoys)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.