[Copy question]:
Given n points in the plane that was all pairwise distinct, a "boomerang" was a tuple of points (i, j, k)
such that th e distance between i
and j
equals the distance between and (the order of the i
k
tuple matters).
Find the number of boomerangs. Assume that n would be is at most and coordinates of points is all in the range [ -10000, 10000] (Inclusi VE).
Example:
Input:[[0,0],[1,0],[2,0]]output:2explanation:the boomerangs is [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]
[Brute force solution]:
Time Analysis:
Spatial Analysis:
[After optimization]:
Time Analysis: n^2
Spatial Analysis:
[Wonderful output CONDITIONS]:
[Wonderful corner case]:
[Thinking questions]:
Do not know how to indicate the distance is equal: The number of occurrences of the statistical distance with HashMap
[a sentence of thought]:
Using HashMap to calculate the number of occurrences of distance, then select 2 points with the combination formula
[input]: null: Normal: Large: Extra Small: Special cases handled in the program: abnormal conditions (unreasonable input):
[Drawing]:
[One brush]:
- It is equivalent to understand the position of the two points of "seeking distance". So the range is the same, all <length.
- When using Getordefault, you should specify the default type in advance, and take a little notice
[Two brushes]:
[Three brushes]:
[Four brushes]:
[Five brushes]:
[Results of five-minute visual debug]:
[Summary]:
"Distance equal" can be understood as "the same distance appears two times", with HashMap
[Complexity]:time Complexity:o (n^2 double loop ) Space complexity:o (n)
[English data structure or algorithm, why not other data structures or algorithms]:
Do not know how distance means: a[0]-b[0], a[1]-b[1] sum of squares
[Key templating code]:
To find the distance:
Public int getdistance (intint[] b) { int dx = a[0]-b[0]; int dy = a[1]-b[1]; return DX * dx + dy * dy; }
[Other solutions]:
[Follow up]:
[The problem given by the LC becomes variable]:
[Code Style]:
classSolution { Public intNumberofboomerangs (int[] points) { //cc if(Points = =NULL|| Points[0] = =NULL) { return0; } //INI HashMapHashmap<integer, integer> map =NewHashmap<>(); intres = 0; //For loop:put into map//Res + = val * (val-1)//. Clear () for(inti = 0; i < points.length; i++) { for(intj = 0; J < Points.length; J + +) { if(i = = j)Continue; intD =getdistance (Points[i], points[j]); Map.put (d, Map.getordefault (d,0) + 1); } for(intval:map.values ()) {Res+ = Val * (val-1); } map.clear (); } //return res returnRes; } Public intGetdistance (int[] A,int[] b) {intDX = a[0]-b[0]; intDY = a[1]-b[1]; returnDX * dx + dy *dy; }}
View Code
447. Number of boomerangs Boomerang array