Description:
You are given a 0-indexed 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.
The Manhattan Distance between two cells (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|.
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.
It can be seen that 12 is the minimum possible maximum distance between any two points after removing exactly one point.
Solution: 最大曼哈顿距离 求给定坐标集合间最大曼哈顿距离
First of all, we do not consider removing exactly one point. This question turn into another question: "calculate maximum Manhattan distance in the set". But how could we get the maximum Manhattan distance? the above links show the proof process.
The Manhattan distance = |Px - Qx| + |Py - Qy|, to remove the absolute value sign, there are four situation:
- Px > Qx and Py > Qy, distance = Px - Qx + Py - Qy = (Px
+Py) - (Qx+Qy) - Px > Qx and Py < Qy, distance = Px - Qx + Qy - Py = (Px
-Py) - (Qx-Qy) - Px < Qx and Py > Qy, distance = Qx - Px + Py - Qy = (
-Px+Py) - (-Qx+Qy) - Px < Qx and Py < Qy, distance = Qx - Px + Qy - Py = (
-Px-Py) - (-Qx-Qy)
The two cases 1 and 4 are the same, 4 is (-Px - Py) - (-Qx - Qy) = (Qx + Qy) - (Px + Py), if Q is great than P, it applies to 4, and P is great than Q, it applies to 1.
The two cases 2 and 3 are the same, (-Px + Py) - (-Qx + Qy) = (Qx - Qy) - (Px - Py)
In conclusion, the maximum Manhatten distance between Point P and Point Q is max((Qx + Qy) - (Px + Py), (Qx - Qy) - (Px - Py)). So we just need two list to store all point's x + y and x - y. then calculate their maximum manhatten distance respectively, and max distance = max(distance of x+y, distance of x-y).
How about the distance of removing exactly one point? we just need remove this point from the x+y list and x-y list and find current maximum distance.
fun minimumDistance(points: Array<IntArray>): Int {
val sum = mutableListOf<Int>()
val diff = mutableListOf<Int>()
points.forEach {
sum.add(it[0] + it[1])
diff.add(it[0] - it[1])
}
// sum[sum.size - 1] - sum[0] is the max distance of x+y collection.
sum.sort()
// diff[diff.size - 1] - diff[0] is the max distance of x-y collection.
diff.sort()
var result = Int.MAX_VALUE
points.forEach {
val itemSum = it[0] + it[1]
val itemDiff = it[0] - it[1]
var max = 0
// remove current itemSum from sum collection.
max = when (itemSum) {
sum[sum.size - 1] -> {
max(max, sum[sum.size - 2] - sum[0])
}
sum[0] -> {
max(max, sum[sum.size - 1] - sum[1])
}
else -> {
max(max, sum[sum.size - 1] - sum[0])
}
}
// remove current itemDiff from diff collection.
max = when (itemDiff) {
diff[diff.size - 1] -> {
max(max, diff[diff.size - 2] - diff[0])
}
diff[0] -> {
max(max, diff[diff.size - 1] - diff[1])
}
else -> {
max(max, diff[diff.size - 1] - diff[0])
}
}
// max is the maximum value of x+y and x-y collecitons.
// calculate the min distance of max distance
result = min(result, max)
}
return result
}