Skip to content

Commit 85fc8b2

Browse files
committed
Add solution #447
1 parent ffd22b8 commit 85fc8b2

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@
355355
443|[String Compression](./0443-string-compression.js)|Medium|
356356
445|[Add Two Numbers II](./0445-add-two-numbers-ii.js)|Medium|
357357
446|[Arithmetic Slices II - Subsequence](./0446-arithmetic-slices-ii-subsequence.js)|Hard|
358+
447|[Number of Boomerangs](./0447-number-of-boomerangs.js)|Medium|
358359
448|[Find All Numbers Disappeared in an Array](./0448-find-all-numbers-disappeared-in-an-array.js)|Easy|
359360
450|[Delete Node in a BST](./0450-delete-node-in-a-bst.js)|Medium|
360361
451|[Sort Characters By Frequency](./0451-sort-characters-by-frequency.js)|Medium|
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 447. Number of Boomerangs
3+
* https://leetcode.com/problems/number-of-boomerangs/
4+
* Difficulty: Medium
5+
*
6+
* You are given n points in the plane that are all distinct, where points[i] = [xi, yi].
7+
* A boomerang is a tuple of points (i, j, k) such that the distance between i and j equals
8+
* the distance between i and k (the order of the tuple matters).
9+
*
10+
* Return the number of boomerangs.
11+
*/
12+
13+
/**
14+
* @param {number[][]} points
15+
* @return {number}
16+
*/
17+
var numberOfBoomerangs = function(points) {
18+
let count = 0;
19+
20+
for (let i = 0; i < points.length; i++) {
21+
const distances = new Map();
22+
for (let j = 0; j < points.length; j++) {
23+
if (i === j) continue;
24+
const d = (points[i][0] - points[j][0]) ** 2 + (points[i][1] - points[j][1]) ** 2;
25+
distances.set(d, (distances.get(d) || 0) + 1);
26+
}
27+
for (const d of distances.values()) {
28+
if (d > 1) count += d * (d - 1);
29+
}
30+
}
31+
32+
return count;
33+
};

0 commit comments

Comments
 (0)