Ctrl K

A Depot Close to All Shops

Hard
A chain of shops is building a depot. Shop i stands at the point shops[i] = [xᵢ, yᵢ] on the plane. The depot can go anywhere on the plane; its coordinates need not be integers. Find the smallest possible sum of the ordinary (Euclidean) distances from the depot to all the shops: √((x - xᵢ)² + (y - yᵢ)²), added up over every i. An answer within 10⁻⁵ of the exact value is accepted. Several shops may stand at the same point.
Example 1
shops=[[0, 0], [6, 0], [0, 6], [6, 6]] → 16.97056274847714

A depot at the centre of the square, (3, 3), is √18 ≈ 4.24264 from each shop, ≈ 16.97056 in total.

Example 2
shops=[[2, 5], [2, 5], [9, 5]] → 7.000000643961637

The best spot is right at (2, 5), where two of the shops are: the third one is then 7 away.

Example 3
shops=[[40, 40]] → 0.0
Constraints
  • 1 ≤ len(shops) ≤ 50
  • len(shops[i]) = 2
  • 0 ≤ xᵢ, yᵢ ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsStars on One Straight Line HardDoes the Robot Cross Its Trail HardRope Around the Posts HardWater as Many Plants as Possible HardBest View for a Turning Camera Hard
loading Python…
Input
shops=[[0, 0], [6, 0], [0, 6], [6, 6]]
Expected
16.97056274847714

Run checks the first 3 tests in your browser. Submit checks all 105 tests on the server, hidden ones included.