File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 355
355
443|[ String Compression] ( ./0443-string-compression.js ) |Medium|
356
356
445|[ Add Two Numbers II] ( ./0445-add-two-numbers-ii.js ) |Medium|
357
357
446|[ Arithmetic Slices II - Subsequence] ( ./0446-arithmetic-slices-ii-subsequence.js ) |Hard|
358
+ 447|[ Number of Boomerangs] ( ./0447-number-of-boomerangs.js ) |Medium|
358
359
448|[ Find All Numbers Disappeared in an Array] ( ./0448-find-all-numbers-disappeared-in-an-array.js ) |Easy|
359
360
450|[ Delete Node in a BST] ( ./0450-delete-node-in-a-bst.js ) |Medium|
360
361
451|[ Sort Characters By Frequency] ( ./0451-sort-characters-by-frequency.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments