Input: points =[[3,10],[5,15],[10,2],[4,4]]Output: 12Explanation:
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`.12is the minimum possible maximum distance between any two points after
removing exactly one point.
The maximum Manhattan distance between any two points is determined by the extreme values of x + y and x - y. By removing one point, we can potentially reduce the spread of these values, thus minimizing the maximum distance.
classSolution {
public:int minimizeManhattan(vector<vector<int>>& points) {
int n = points.size();
vector<int> a, b;
for (auto& p : points) {
a.push_back(p[0] + p[1]);
b.push_back(p[0] - p[1]);
}
auto get_extremes = [](const vector<int>& v, int skip) {
int mx = INT_MIN, mn = INT_MAX;
for (int i =0; i < v.size(); ++i) {
if (i == skip) continue;
mx = max(mx, v[i]);
mn = min(mn, v[i]);
}
return mx - mn;
};
int ans = INT_MAX;
for (int i =0; i < n; ++i) {
int d1 = get_extremes(a, i);
int d2 = get_extremes(b, i);
ans = min(ans, max(d1, d2));
}
return ans;
}
};
classSolution {
publicintminimizeManhattan(int[][] points) {
int n = points.length;
int[] a =newint[n], b =newint[n];
for (int i = 0; i < n; i++) {
a[i]= points[i][0]+ points[i][1];
b[i]= points[i][0]- points[i][1];
}
int ans = Integer.MAX_VALUE;
for (int skip = 0; skip < n; skip++) {
int mx1 = Integer.MIN_VALUE, mn1 = Integer.MAX_VALUE;
int mx2 = Integer.MIN_VALUE, mn2 = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (i == skip) continue;
mx1 = Math.max(mx1, a[i]); mn1 = Math.min(mn1, a[i]);
mx2 = Math.max(mx2, b[i]); mn2 = Math.min(mn2, b[i]);
}
ans = Math.min(ans, Math.max(mx1 - mn1, mx2 - mn2));
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funminimizeManhattan(points: Array<IntArray>): Int {
val n = points.size
val a = IntArray(n) { points[it][0] + points[it][1] }
val b = IntArray(n) { points[it][0] - points[it][1] }
var ans = Int.MAX_VALUE
for (skip in0 until n) {
var mx1 = Int.MIN_VALUE; var mn1 = Int.MAX_VALUE
var mx2 = Int.MIN_VALUE; var mn2 = Int.MAX_VALUE
for (i in0 until n) {
if (i == skip) continue mx1 = maxOf(mx1, a[i]); mn1 = minOf(mn1, a[i])
mx2 = maxOf(mx2, b[i]); mn2 = minOf(mn2, b[i])
}
ans = minOf(ans, maxOf(mx1 - mn1, mx2 - mn2))
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
defminimize_manhattan(points: list[list[int]]) -> int:
n = len(points)
a = [x + y for x, y in points]
b = [x - y for x, y in points]
ans = float('inf')
for skip in range(n):
mx1 = max(a[i] for i in range(n) if i != skip)
mn1 = min(a[i] for i in range(n) if i != skip)
mx2 = max(b[i] for i in range(n) if i != skip)
mn2 = min(b[i] for i in range(n) if i != skip)
ans = min(ans, max(mx1 - mn1, mx2 - mn2))
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
impl Solution {
pubfnminimize_manhattan(points: Vec<Vec<i32>>) -> i32 {
let n = points.len();
let a: Vec<i32>= points.iter().map(|p| p[0] + p[1]).collect();
let b: Vec<i32>= points.iter().map(|p| p[0] - p[1]).collect();
letmut ans =i32::MAX;
for skip in0..n {
let mx1 = a.iter().enumerate().filter(|(i, _)|*i != skip).map(|(_, v)|*v).max().unwrap();
let mn1 = a.iter().enumerate().filter(|(i, _)|*i != skip).map(|(_, v)|*v).min().unwrap();
let mx2 = b.iter().enumerate().filter(|(i, _)|*i != skip).map(|(_, v)|*v).max().unwrap();
let mn2 = b.iter().enumerate().filter(|(i, _)|*i != skip).map(|(_, v)|*v).min().unwrap();
ans = ans.min(mx1 - mn1).max(mx2 - mn2);
}
ans
}
}