Best Position for a Service Centre

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] = [xi, yi] 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 [xcentre, ycentre] such that the sum of distances from [xcentre, ycentre] to every customer position is minimized.

Answers within 10^-5 of the actual value will be accepted.

Example 1
Inputpositions = [[0,1],[1,0],[1,2],[2,1]]
Output4
Choosing [xcentre, ycentre] = [1, 1] makes the distance to each customer equal to 1, so the minimum possible sum is 4.
Example 2
Inputpositions = [[1,1],[3,3]]
Output2.82843
The minimum possible sum of distances is sqrt(2) + sqrt(2) = 2.82843.

Constraints

  • 1 <= positions.length <= 50
  • positions[i].length == 2
  • 0 <= xi, yi <= 100

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate