-
Notifications
You must be signed in to change notification settings - Fork 16
/
NumberOfBoomerangs.java
37 lines (30 loc) · 1.02 KB
/
NumberOfBoomerangs.java
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
import java.util.HashMap;
import java.util.Map;
public class NumberOfBoomerangs {
public int numberOfBoomerangs(int[][] points) {
if (points.length == 0) {
return 0;
}
int boomerangs = 0;
Map<Integer, Integer> distances = new HashMap<>();
for (int i = 0; i < points.length; i++) {
for (int j = 0 ; j < points.length; j++) {
if (i == j) {
continue;
}
int distance = getDistance(points[i], points[j]);
distances.put(distance, distances.getOrDefault(distance, 0) + 1);
}
for (int frequency : distances.values()) {
boomerangs += frequency * (frequency - 1);
}
distances.clear();
}
return boomerangs;
}
private int getDistance(int[] point1, int[] point2) {
int x_diff = point1[0] - point2[0];
int y_diff = point1[1] - point2[1];
return x_diff * x_diff + y_diff * y_diff;
}
}