[LeetCode] Number of Boomerangs

447. Number of Boomerangs

You are given n points in the plane that are all distinct, where points[i] = [xi, yi]. A boomerang is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).

Return the number of boomerangs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
int getDistance(vector<int>& v1, vector<int>& v2) {
return (v1[0] - v2[0]) * (v1[0] - v2[0]) + (v1[1] - v2[1]) * (v1[1] - v2[1]);
}
public:
int numberOfBoomerangs(vector<vector<int>>& points) {
int res = 0;
for(int i = 0; i < points.size(); i++) {
unordered_map<int, int> lookup;
for(int j = 0; j < points.size(); j++) {
if(i == j) continue;
int distance = getDistance(points[i], points[j]);
lookup[distance]++;
}
for(auto [_, v]: lookup)
res += v * (v-1);

}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/20/PS/LeetCode/number-of-boomerangs/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.