Best Position for a Service Centre Problem
Description
LeetCode Problem 1515.
A delivery company wants to build a new service center in a new city. The company knows the positions of all the customers in this city on a 2D-Map and wants to build the new center in a position such that the sum of the euclidean distances to all customers is minimum.
Given an array positions where positions[i] = [x_i, y_i] is the position of the ith customer on the map, return the minimum sum of the euclidean distances to all customers.
In other words, you need to choose the position of the service center [x_centre, y_centre] such that the following formula is minimized:

Answers within 10^(-5) of the actual value will be accepted.
Example 1:

Input: positions = [[0,1],[1,0],[1,2],[2,1]]
Output: 4.00000
Explanation: As shown, you can see that choosing [x_centre, y_centre] = [1, 1] will make the distance to each customer = 1, the sum of all distances is 4 which is the minimum possible we can achieve.
Example 2:

Input: positions = [[1,1],[3,3]]
Output: 2.82843
Explanation: The minimum possible sum of distances = sqrt(2) + sqrt(2) = 2.82843
Constraints:
1 <= positions.length <= 50positions[i].length == 20 <= x_i, y_i <= 100
Solution
Approach: Gradient Descent with a Shrinking Step
The point we are looking for is the geometric median of the customers. There is no formula for it, but the function we minimize, f(x, y) = sum of the distances from (x, y) to each customer, is convex: it has a single valley and no misleading local minima. So we can walk downhill until we stop moving.
The gradient of one term sqrt((x - xi)^2 + (y - yi)^2) is the unit vector pointing from that customer to (x, y), that is ((x - xi) / d, (y - yi) / d) where d is the distance. The gradient of f is the sum of these unit vectors. Then:
- Start at the centroid of the customers, which is already close to the answer.
- Compute the gradient and the current sum of distances, and step against the gradient:
x -= step * gradX,y -= step * gradY. - Multiply
stepby0.999after each move. Big steps early get us near the answer quickly, and the shrinking steps let us settle instead of bouncing around the minimum forever. - Stop when both components of the step are at most
1e-6, and return the sum of distances.
The tricky case is when (x, y) lands exactly on a customer: d = 0 and the unit vector is 0 / 0. Adding a tiny 1e-8 to the denominator avoids that. When the true optimum is on a customer (for example when most customers share one spot), the gradient never becomes zero there, but the step keeps shrinking, so the walk still settles within the accepted error.
Complexity: each iteration takes O(n) time. The gradient has length at most n, so the step step * gradient is below 1e-6 once 0.5 * 0.999^k * 50 <= 1e-6, after at most about 17,000 iterations (often fewer). That is O(n · K) time with K ≈ 1.7 · 104, and O(1) extra space.
import math
class Solution:
def getMinDistSum(self, positions: List[List[int]]) -> float:
n = len(positions)
# Start at the centroid and walk against the gradient with a shrinking step.
x = sum(p[0] for p in positions) / n
y = sum(p[1] for p in positions) / n
step, decay, eps = 0.5, 0.999, 1e-6
while True:
grad_x = grad_y = dist = 0.0
for px, py in positions:
a, b = x - px, y - py
d = math.sqrt(a * a + b * b)
# the gradient of one distance is the unit vector from the customer; 1e-8 avoids 0 / 0
grad_x += a / (d + 1e-8)
grad_y += b / (d + 1e-8)
dist += d
dx, dy = grad_x * step, grad_y * step
if abs(dx) <= eps and abs(dy) <= eps:
return dist
x -= dx
y -= dy
step *= decay