#1515

Best Position for a Service Centre

international master · 1900 · lc hard +32 · verified · 35.1% accepted · 246 likes · top 13%

Description

A delivery company must site a new service center to minimize total Euclidean distance to all customers. Given positions[i] = [xi, yi] for each customer, find the optimal center location and return the minimum achievable total distance. Answers within 10-5 of the true value are accepted.

Example 1:

Input: positions = [[0,1],[1,0],[1,2],[2,1]]
Output: 4.00000
Explanation: As shown, you can see that choosing [xcentre, ycentre] = [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

Code

1
2
3