Number of Boomerangs LeetCode solution in C++

1
class Solution {
2
public:
3
int numberOfBoomerangs(vector<vector<int>> &points) {
4
int cnt = 0, n = points.size();
5
for (int i = 0; i < n; i++) {
6
map<int, int> mp;
7
for (int j = 0; j < n; j++) {
8
if (i == j) continue;
9

10
int tmp = findDistance(points[i], points[j]);
11
if (mp.find(tmp) != mp.end())
12
cnt += mp[tmp] * 2; // 2 is multiplied bcoz the order of j & k can be k & j also
13

14
mp[tmp]++;
15
}
16
}
17

18
return cnt;
19
}
20

21
int findDistance(vector<int> &p1, vector<int> &p2) {
22
return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);
23
}
24
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0