-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path447.cpp
47 lines (47 loc) · 1.26 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
43
44
45
46
47
// hashmap+beat97%.cpp
class Solution {
public:
int numberOfBoomerangs(vector<pair<int, int>> &points) {
int n = points.size(), count = 0;
unordered_map<int, int> map;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (j == i)
continue;
map[dist(points[i], points[j])]++;
}
for (auto it = map.begin(); it != map.end(); ++it)
count += (it->second) * (it->second - 1);
map.clear();
}
return count;
}
int dist(pair<int, int> &i, pair<int, int> &j) {
int x = i.first - j.first;
int y = i.second - j.second;
return x * x + y * y;
}
};
// TLE.cpp
class Solution2 {
public:
int numberOfBoomerangs(vector<pair<int, int>> &points) {
int n = points.size(), count = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (j == i)
continue;
else
for (int k = 0; k < n; ++k)
if (k == i || k == j)
continue;
else if (dist(i, j, points) == dist(i, k, points))
count++;
return count;
}
int dist(int i, int j, vector<pair<int, int>> &points) {
int x = points[i].first - points[j].first;
int y = points[i].second - points[j].second;
return x * x + y * y;
}
};