FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/java/447_Number_of_Boomerangs.java at master · jakehoare/leetcode · GitHub
jakehoare
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
30
Star
51
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
java
/
447_Number_of_Boomerangs.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
39 lines (29 loc) · 1.26 KB
Breadcrumbs
leetcode
/
java
/
447_Number_of_Boomerangs.java
Copy path
File metadata and controls
39 lines (29 loc) · 1.26 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
https://leetcode.com/problems/number-of-boomerangs/
Given n points in the plane that are all pairwise distinct, 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).
Find the number of boomerangs.
For each point, find distances from each other points and count by distance. For each distance, boomerang from every
point at that distance to every other point.
Time - O(n**2)
Space - O(n)
*/
public
class
Solution
{
public
int
numberOfBoomerangs
(
int
[][]
points
) {
int
boomerangs
=
0
;
Map
<
Integer
,
Integer
>
distanceCounts
=
new
HashMap
<>();
for
(
int
i
=
0
;
i
<
points
.
length
; ++
i
) {
for
(
int
j
=
0
;
j
<
points
.
length
; ++
j
) {
if
(
i
==
j
)
continue
;
int
dist
= (
points
[
i
][
0
] -
points
[
j
][
0
]) * (
points
[
i
][
0
] -
points
[
j
][
0
]);
dist
+= (
points
[
i
][
1
] -
points
[
j
][
1
]) * (
points
[
i
][
1
] -
points
[
j
][
1
]);
distanceCounts
.
put
(
dist
,
distanceCounts
.
getOrDefault
(
dist
,
0
) +
1
);
}
for
(
int
count
:
distanceCounts
.
values
())
boomerangs
+=
count
* (
count
-
1
);
distanceCounts
.
clear
();
}
return
boomerangs
;
}
}
Back
|
FazBrowse Home
|
New Git URL