<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 findDistance(double x1, double y1, double x2, double y2)
    {
        double x = (x1 - x2) * (x1 - x2);
        double y = (y1 - y2) * (y1 - y2);
        return sqrt(x + y);
    }
public:
    double getMinDistSum(vector<vector<int>>& positions) {
        double minDistance = DBL_MAX;
        vector<pair<double, double>> maximas;
        for (int i = 0; i <= 100; i++)
        {
            for (int j = 0; j <= 100; j++)
            {
                double distance = 0;
                for (int k = 0; k < positions.size(); k++)
                {
                    distance += findDistance(i, j, positions[k][0], positions[k][1]);
                }
                if (distance < minDistance)
                {
                    maximas.clear();
                    maximas.push_back(make_pair(i, j));
                    minDistance = distance;
                }
                else if (distance == minDistance)
                {
                    maximas.push_back(make_pair(i, j));
                }
            }
        }
        vector<pair<double, double>> secondMaximas;
        for (int i = 0; i < maximas.size(); i++)
        {
            // cout<<maximas[i].first<<" "<<maximas[i].second<<"\n";
            double xStart = maximas[i].first - 1;
            double xEnd = maximas[i].first + 1;
            double diff = 0.01;
            for (double j = xStart; j < xEnd; j += diff)
            {
                double yStart = maximas[i].second - 1;
                double yEnd = maximas[i].second + 1;
                for (double k = yStart; k < yEnd; k += diff)
                {
                    double distance = 0;
                    for (int l = 0; l < positions.size(); l++)
                    {
                        distance += findDistance(j, k, positions[l][0], positions[l][1]);
                    }

                    if (distance < minDistance)
                    {
                        secondMaximas.clear();
                        secondMaximas.push_back(make_pair(j, k));
                        minDistance = distance;
                    }
                    else if (distance == minDistance)
                    {
                        secondMaximas.push_back(make_pair(j, k));
                    }
                }
            }
        }

        vector<pair<double, double>> thirdMaximas;
        for (int i = 0; i < secondMaximas.size(); i++)
        {
            // cout<<maximas[i].first<<" "<<maximas[i].second<<"\n";
            double xStart = secondMaximas[i].first - 0.01;
            double xEnd = secondMaximas[i].first + 0.01;
            double diff = 0.0001;
            for (double j = xStart; j < xEnd; j += diff)
            {
                double yStart = secondMaximas[i].second - 0.01;
                double yEnd = secondMaximas[i].second + 0.01;
                for (double k = yStart; k < yEnd; k += diff)
                {
                    double distance = 0;
                    for (int l = 0; l < positions.size(); l++)
                    {
                        distance += findDistance(j, k, positions[l][0], positions[l][1]);
                    }

                    if (distance < minDistance)
                    {
                        thirdMaximas.clear();
                        thirdMaximas.push_back(make_pair(j, k));
                        minDistance = distance;
                    }
                    else if (distance == minDistance)
                    {
                        thirdMaximas.push_back(make_pair(j, k));
                    }
                }
            }
        }
        return minDistance;
    }
};
11700