<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 {
    double delta = 1e-5;
public:
    double getAllDistance(double x, double y, const vector<vector<int>> & positions) {
        double distance = 0.;
        for(auto & position : positions) {
            
            double lengthX = abs(x-(double)position[0]);
            double lengthY = abs(y-(double)position[1]);
            distance += (lengthX == 0 || lengthY == 0) ? max(lengthX, lengthY) : hypot(lengthX, lengthY);
        }

        return distance;
    }

    // function to calculate optimal distance when x is fixed and y varies from minY to maxY
    double getOptimalDistance(double x, double minY, double maxY, vector<vector<int>>& positions)
    {
        // binary search on optimal value of y
        double minDistance = numeric_limits<double>::max();

        while(minY <= maxY)
        {
            double midY = minY + (maxY-minY)/2.0;
            double dist = getAllDistance(x, midY, positions);
            double distl = getAllDistance(x, midY - delta, positions);

            if(distl <= dist) {
                minDistance = distl;
                maxY = midY-delta;
            }
            else {
                minDistance = dist;
                minY = midY+delta;
            }

            minDistance = min({minDistance, dist, distl});
        }

        return minDistance;
    }

    double getMinDistSum(vector<vector<int>>& positions) {
        double lX = numeric_limits<double>::max();
        double rX = numeric_limits<double>::min();
        double lY = numeric_limits<double>::max();
        double rY = numeric_limits<double>::min();

        // This defines the boundaries of the search
        for(auto & p : positions) {
            lX = min(lX, (double)p[0]);
            rX = max(rX, (double)p[0]);
            lY = min(lY, (double)p[1]);
            rY = max(rY, (double)p[1]);
        }

        // Search the minimum x with the eval fonction being computeMinY
        // To find the minimum x, search the first value where computeMinY(x) < computeMinY(x+delta) is true

        double minX = numeric_limits<double>::max();
        while(lX <= rX) {
            double midX = lX + (rX - lX)/2.0;

            double dist = getOptimalDistance(midX, lY, rY, positions);
            double distl = getOptimalDistance(midX-delta, lY, rY, positions);

            if(distl <= dist) {
                minX = dist;
                rX = midX-delta;
            }
            else {
                minX = distl;
                lX = midX+delta;
            }
        }

        return min(getOptimalDistance(lX, lY, rY, positions), getOptimalDistance(rX, lY, rY, positions));
        
    }
};
10400