<p>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 <strong>the sum of the euclidean distances to all customers is minimum</strong>.</p>

<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>

<p>In other words, you need to choose the position of the service center <code>[x<sub>centre</sub>, y<sub>centre</sub>]</code> such that the following formula is minimized:</p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/25/q4_edited.jpg" />
<p>Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/25/q4_e1.jpg" style="width: 377px; height: 362px;" />
<pre>
<strong>Input:</strong> positions = [[0,1],[1,0],[1,2],[2,1]]
<strong>Output:</strong> 4.00000
<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.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/25/q4_e3.jpg" style="width: 419px; height: 419px;" />
<pre>
<strong>Input:</strong> positions = [[1,1],[3,3]]
<strong>Output:</strong> 2.82843
<strong>Explanation:</strong> The minimum possible sum of distances = sqrt(2) + sqrt(2) = 2.82843
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= positions.length &lt;= 50</code></li>
	<li><code>positions[i].length == 2</code></li>
	<li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 100</code></li>
</ul>

class Solution {
public:
    double dist(double x1, double y1, double x2, double y2){
        return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
    }

    double getDistFromPoints(vector<vector<int>>& positions, double x, double y){
        double distance = 0;
        for (auto p : positions){
            distance += dist(p[0],p[1],x,y);
        }
        return distance;
    }

    const int dirs[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};

    double getMinDistSum(vector<vector<int>>& positions) {
        if (positions.size() == 1) return (double)0;
        double x = 0;
        double y = 0;

        int n = positions.size();
        for (auto pos : positions){
            x += pos[0];
            y += pos[1];
        }
        x = (double)x/n;
        y = (double)y/n;
        double ans = getDistFromPoints(positions,x,y);
        //starting coordinates

        double eps = 1e-6;  // controls the precision - give answer accurate to 5 decimals
        double step = 100.0;

        // done is used to keep track of updates: if none of the neighbors of the current
        // point that are *step* steps away improve the solution, then *step* is too big
        // and we need to look closer to the current point, so we must half *step*.
        bool found = false;

        // while we still need a more precise answer
        while(step > eps){
            found = false;

            for (int i = 0; i < 4; ++i){
                double new_x = x + step * dirs[i][0];
                double new_y = y + step * dirs[i][1];
                double newdist = getDistFromPoints(positions,new_x,new_y);
                if (newdist < ans){
                    ans = newdist;
                    x = new_x;
                    y = new_y;

                    // an improvement has been made, so
                    // don't half step in the next iteration, because we might need
                    // to jump the same amount again
                    found = true;
                    break;
                }
            }

            // if not found smaller distance by moving around
            // our step is too big, we need to look closer
            if (!found){
                step /= 2;
            }
        }

        return ans;
    }
};
22600