forked from vns9/binarysearch.com
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPoint Distances with Shared Coordinate.cpp
41 lines (40 loc) · 1.32 KB
/
Point Distances with Shared Coordinate.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
34
35
36
37
38
39
40
41
// https://binarysearch.com/problems/Point-Distances-with-Shared-Coordinate
int mindis(int i, vector<int>& v) {
int lo = 0;
int hi = v.size() - 1;
int currdiff = INT_MAX;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (v[mid] < i) {
currdiff = min(currdiff, (int)abs(v[mid] - i));
lo = mid + 1;
} else if (v[mid] > i) {
currdiff = min(currdiff, (int)abs(v[mid] - i));
hi = mid - 1;
} else {
if (mid - 1 >= 0) currdiff = min(currdiff, (int)abs(v[mid - 1] - i));
if (mid + 1 < v.size()) currdiff = min(currdiff, (int)abs(v[mid + 1] - i));
break;
}
}
return currdiff;
}
vector<int> solve(vector<vector<int>>& points) {
unordered_map<int, vector<int>> xtoy;
unordered_map<int, vector<int>> ytox;
for (auto point : points) {
xtoy[point[0]].push_back(point[1]);
ytox[point[1]].push_back(point[0]);
}
for (auto kv : xtoy) {
sort(xtoy[kv.first].begin(), xtoy[kv.first].end());
}
for (auto kv : ytox) {
sort(ytox[kv.first].begin(), ytox[kv.first].end());
}
vector<int> manhat;
for (auto point : points) {
manhat.push_back(min(mindis(point[1], xtoy[point[0]]), mindis(point[0], ytox[point[1]])));
}
return manhat;
}