-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path447.cpp
42 lines (32 loc) · 1.15 KB
/
447.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
42
class Solution {
public:
int numberOfBoomerangs(vector<pair<int, int>>& points) {
int res = 0;
// iterate over all the points
for (int i = 0; i < points.size(); ++i) {
unordered_map<long, int> group(points.size());
// iterate over all points other than points[i]
for (int j = 0; j < points.size(); ++j) {
if (j == i) continue;
int dy = points[i].second - points[j].second;
int dx = points[i].first - points[j].first;
// compute squared euclidean distance from points[i]
int key = dy * dy;
key += dx * dx;
// accumulate # of such "j"s that are "key" distance from "i"
++group[key];
}
for (auto& p : group) {
if (p.second > 1) {
/*
* for all the groups of points,
* number of ways to select 2 from n =
* nP2 = n!/(n - 2)! = n * (n - 1)
*/
res += p.second * (p.second - 1);
}
}
}
return res;
}
};