-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathminimize-manhattan-distances.cpp
33 lines (30 loc) · 1.25 KB
/
minimize-manhattan-distances.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Time: O(n)
// Space: O(1)
// math
class Solution {
public:
int minimumDistance(vector<vector<int>>& points) {
const auto& max_distance = [&](int exclude) {
static const int POS_INF = numeric_limits<int>::max();
static const int NEG_INF = numeric_limits<int>::min();
pair<int, int> max_sum = {NEG_INF, -1};
pair<int, int> min_sum = {POS_INF, -1};
pair<int, int> max_diff = {NEG_INF, -1};
pair<int, int> min_diff = {POS_INF, -1};
for (int i = 0; i < size(points); ++i) {
if (i == exclude) {
continue;
}
const int x = points[i][0], y = points[i][1];
max_sum = max(max_sum, {x + y, i});
min_sum = min(min_sum, {x + y, i});
max_diff = max(max_diff, {x - y, i});
min_diff = min(min_diff, {x - y, i});
}
return max(tuple{max_sum.first - min_sum.first, max_sum.second, min_sum.second},
tuple{max_diff.first - min_diff.first, max_diff.second, min_diff.second});
};
const auto& [_, i, j] = max_distance(-1);
return min(get<0>(max_distance(i)), get<0>(max_distance(j)));
}
};