|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../path-with-maximum-probability "Path with Maximum Probability") |
| 9 | + |
| 10 | +Next > |
| 11 | + |
| 12 | +## [1515. Best Position for a Service Centre (Hard)](https://leetcode.com/problems/best-position-for-a-service-centre "服务中心的最佳位置") |
| 13 | + |
| 14 | +<p>A delivery company wants to build a new service centre 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 centre in a position such that <strong>the sum of the euclidean distances to all customers is minimum</strong>.</p> |
| 15 | + |
| 16 | +<p>Given an array <code>positions</code> where <code>positions[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> is the position of the <code>ith</code> customer on the map, return <em>the minimum sum of the euclidean distances</em> to all customers.</p> |
| 17 | + |
| 18 | +<p>In other words, you need to choose the position of the service centre <code>[x<sub>centre</sub>, y<sub>centre</sub>]</code> such that the following formula is minimized:</p> |
| 19 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/06/25/q4_edited.jpg" /> |
| 20 | +<p>Answers within <code>10^-5</code> of the actual value will be accepted.</p> |
| 21 | + |
| 22 | +<p> </p> |
| 23 | +<p><strong>Example 1:</strong></p> |
| 24 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/06/25/q4_e1.jpg" style="width: 377px; height: 362px;" /> |
| 25 | +<pre> |
| 26 | +<strong>Input:</strong> positions = [[0,1],[1,0],[1,2],[2,1]] |
| 27 | +<strong>Output:</strong> 4.00000 |
| 28 | +<strong>Explanation:</strong> As shown, you can see that choosing [x<sub>centre</sub>, y<sub>centre</sub>] = [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. |
| 29 | +</pre> |
| 30 | + |
| 31 | +<p><strong>Example 2:</strong></p> |
| 32 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/06/25/q4_e3.jpg" style="width: 419px; height: 419px;" /> |
| 33 | +<pre> |
| 34 | +<strong>Input:</strong> positions = [[1,1],[3,3]] |
| 35 | +<strong>Output:</strong> 2.82843 |
| 36 | +<strong>Explanation:</strong> The minimum possible sum of distances = sqrt(2) + sqrt(2) = 2.82843 |
| 37 | +</pre> |
| 38 | + |
| 39 | +<p><strong>Example 3:</strong></p> |
| 40 | + |
| 41 | +<pre> |
| 42 | +<strong>Input:</strong> positions = [[1,1]] |
| 43 | +<strong>Output:</strong> 0.00000 |
| 44 | +</pre> |
| 45 | + |
| 46 | +<p><strong>Example 4:</strong></p> |
| 47 | + |
| 48 | +<pre> |
| 49 | +<strong>Input:</strong> positions = [[1,1],[0,0],[2,0]] |
| 50 | +<strong>Output:</strong> 2.73205 |
| 51 | +<strong>Explanation:</strong> At the first glance, you may think that locating the centre at [1, 0] will achieve the minimum sum, but locating it at [1, 0] will make the sum of distances = 3. |
| 52 | +Try to locate the centre at [1.0, 0.5773502711] you will see that the sum of distances is 2.73205. |
| 53 | +Be careful with the precision! |
| 54 | +</pre> |
| 55 | + |
| 56 | +<p><strong>Example 5:</strong></p> |
| 57 | + |
| 58 | +<pre> |
| 59 | +<strong>Input:</strong> positions = [[0,1],[3,2],[4,5],[7,6],[8,9],[11,1],[2,12]] |
| 60 | +<strong>Output:</strong> 32.94036 |
| 61 | +<strong>Explanation:</strong> You can use [4.3460852395, 4.9813795505] as the position of the centre. |
| 62 | +</pre> |
| 63 | + |
| 64 | +<p> </p> |
| 65 | +<p><strong>Constraints:</strong></p> |
| 66 | + |
| 67 | +<ul> |
| 68 | + <li><code>1 <= positions.length <= 50</code></li> |
| 69 | + <li><code>positions[i].length == 2</code></li> |
| 70 | + <li><code>0 <= positions[i][0], positions[i][1] <= 100</code></li> |
| 71 | +</ul> |
| 72 | + |
| 73 | +### Related Topics |
| 74 | + [[Geometry](../../tag/geometry/README.md)] |
| 75 | + |
| 76 | +### Hints |
| 77 | +<details> |
| 78 | +<summary>Hint 1</summary> |
| 79 | +The problem can be reworded as, giving a set of points on a 2d-plane, return the geometric median. |
| 80 | +</details> |
| 81 | + |
| 82 | +<details> |
| 83 | +<summary>Hint 2</summary> |
| 84 | +Loop over each triplet of points (positions[i], positions[j], positions[k]) where i < j < k, get the centre of the circle which goes throw the 3 points, check if all other points lie in this circle. |
| 85 | +</details> |
0 commit comments