KatInterview

Optimal 1D Coverage: Minimize Worst-Case Distance to k Centers

Greedy & IntervalsAuto-graded · 22 test cases
MediumAuto-gradedGreedy & Intervalsbinary-searchgreedysorting

Last updated

Tests binary-search-on-answer combined with a greedy interval feasibility check to solve the 1D k-center minimax placement problem.

Optimal 1D Coverage: Minimize Worst-Case Distance to k Centers

You have a list of points on a number line and a budget of k centers you can place anywhere on the line. The coverage radius of a placement is the largest absolute distance any point must travel to reach its nearest center. Your goal is to choose the positions of the k centers to make that worst-case distance as small as possible.

Return a dictionary with two keys:

  • "radius": the minimized worst-case distance r* (a float; it will always be a multiple of 0.5).
  • "centers": a list of exactly k floats giving center positions that achieve r*, produced by a left-to-right greedy sweep. If fewer than k centers are needed, repeat the last center until the list has length k.

How placement works

A single center at position c with radius r covers every point whose distance to c is at most r, i.e., every point in the interval [c − r, c + r]. You want all n points covered by the union of the k such intervals, with r as small as possible.

Determining which centers to output

Once the optimal radius is known, use the following greedy left-to-right rule to find the centers:

  1. Sort the points.
  2. Find the leftmost uncovered point p.
  3. Place a center at p + r*, covering everything in [p, p + 2r*].
  4. Repeat from step 2 until all points are covered.

This greedy sweep produces the centers in the required order.

Constraints

  • 1 ≤ k ≤ n ≤ 2 × 10⁵
  • -10⁹ ≤ points[i] ≤ 10⁹
  • Point values are integers and may repeat.
  • r* is always an exact multiple of 0.5; the returned "radius" must match this exactly.
  • The returned "centers" list must have length exactly k.

Examples

Example 1

Input: place_centers_min_radius([1, 2, 3], 1)

Output: {'radius': 1, 'centers': [2]}

Example 2

Input: place_centers_min_radius([0, 0, 10, 10], 2)

Output: {'radius': 0, 'centers': [0, 10]}

Example 3

Input: place_centers_min_radius([-5, -4, 10], 2)

Output: {'radius': 0.5, 'centers': [-4.5, 10.5]}

Example 4

Input: place_centers_min_radius([0, 100], 1)

Output: {'radius': 50, 'centers': [50]}

Hints

Hint 1

Sort the points first. The optimal radius only depends on the span of each greedy group, so think about binary-searching on twice the radius (an integer) rather than on the radius itself.

Hint 2

For a fixed candidate doubled-radius R2, a greedy left-to-right sweep decides feasibility in O(n): anchor an interval at the leftmost uncovered point, skip everything it covers, and count intervals used. If the count is ≤ k, R2 is feasible.

Hint 3

To avoid all floating-point issues, binary-search over integer values of 2r on [0, 2*(max − min)]. Every comparison in the greedy stays between integers. Divide by 2.0 only at the very end when writing the output.