Skip to main content

Minimize Manhattan Distances

Problem Statement​

You are given a array points representing integer coordinates of some points on a 2D plane, where points[i] = [xi, yi].

The distance between two points is defined as their Manhattan distance.

Return the minimum possible value for maximum distance between any two points by removing exactly one point.

Example 1:

Input: points = [[3,10],[5,15],[10,2],[4,4]] Output: 12

Explanation: The maximum distance after removing each point is the following:

  • After removing the 0th point the maximum distance is between points (5, 15) and (10, 2), which is |5 - 10| + |15 - 2| = 18.

  • After removing the 1st point the maximum distance is between points (3, 10) and (10, 2), which is |3 - 10| + |10 - 2| = 15.

  • After removing the 2nd point the maximum distance is between points (5, 15) and (4, 4), which is |5 - 4| + |15 - 4| = 12.

  • After removing the 3rd point the maximum distance is between points (5, 15) and (10, 2), which is |5 - 10| + |15 - 2| = 18.

  • 12 is the minimum possible maximum distance between any two points after removing exactly one point.

Example 2:

Input: points = [[1,1],[1,1],[1,1]] Output: 0

Explanation: Removing any of the points results in the maximum distance between any two points of 0.

Constraints: 3 <= points.length <= 105 points[i].length == 2 1 <= points[i][0], points[i][1] <= 108

Solutions:​

Approach​

  1. Manhattan Distance between any two points (x1,y1) & (x2,y2) is:

    |x1–x2|+|y1–y2|=max(x1–x2-y1+y2,-x1+x2+y1–y2,-x1+x2–y1+y2,x1–x2+y1–y2)

  2. The above expression can also be formulated as: |x1–x2|+|y1–y2|=max((x1–y1)–(x2–y2),(-x1+y1)–(-x2+y2),(-x1–y1)–(-x2–y2),(x1+y1)–(x2+y2))

  3. From the above expression, it's apparent that the Maximum Manhattan Distance can be determined by storing the sums and differences of the coordinates.

  4. Consider 2 set of pairs st1,st2 where st1 will store the sum of X,Y coordinate along with the index of the point & st2 will store the difference of X,Y coordinates along with the index of the point

  5. Now, iterate over the Points again, & Calculate the sum x + y and difference x - y for the current element.

  6. Check if these calculated sums and differences exist in the sets. If they do, erase them.

  7. To compute the maximum manhattan distance (maxi) after removing each point:

    maxi=max(maxsum-minsum,maxdiff-mindiff);

    • Now, find the values of minsum, maxsum, mindiff, maxdiff

    • minsum=st1.begin(), maxsum=--st1.end()

    • mindiff=st2.begin(), maxdiff=--st2.end()

    Note:- st.end() denotes an iterator pointing to the position just after the last element that's why we have taken --st.end()

    1. After finding the maximum value upon removal of the current sum and difference, store the minimum computed so far in variable mini which holds the minimum of all Manhattan Distance found so far.

    2. Add the removed sum and differences to the sets st1 and st2 to consider the next sum and differnce.

    3. Finally after iterating over all the points return minimum of all computed values.

code:​

Written by @Ajay-Dhangar
#include <vector>
#include <set>
#include <limits.h>
using namespace std;

class Solution {
public:
int minimumDistance(vector<vector<int>>& A) {
set<pair<int, int>> st1, st2;
for (int i = 0; i < A.size(); i++) {
st1.insert({A[i][0] + A[i][1], i});
st2.insert({A[i][0] - A[i][1], i});
}

int mini = INT_MAX;
for (int i = 0; i < A.size(); i++) {
int t1 = A[i][0] + A[i][1];
int t2 = A[i][0] - A[i][1];
if (st1.find({t1, i}) != st1.end()) st1.erase({t1, i});
if (st2.find({t2, i}) != st2.end()) st2.erase({t2, i});

int minsum = st1.begin()->first;
int maxsum = (--st1.end())->first;
int mindiff = st2.begin()->first;
int maxdiff = (--st2.end())->first;
int maxi = max(maxsum - minsum, maxdiff - mindiff);
mini = min(mini, maxi);

st1.insert({t1, i});
st2.insert({t2, i});
}

return mini;
}
};

Complexity​

Time complexity: O(nlogn)O(nlogn)

Space complexity: O(n)O(n)