-
Notifications
You must be signed in to change notification settings - Fork 894
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Solution to Number of Boomerangs
- Loading branch information
Showing
2 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/number-of-boomerangs/ | ||
* Primary idea: traverse the array and compare distance one by one | ||
* | ||
* Time Complexity: O(n^2), Space Complexity: O(n) | ||
* | ||
*/ | ||
|
||
class NumberBoomerangs { | ||
func numberOfBoomerangs(_ points: [[Int]]) -> Int { | ||
var num = 0 | ||
|
||
for (i, point) in points.enumerated() { | ||
var dict = [Int: Int]() | ||
for (j, anotherpoint) in points.enumerated() { | ||
if i == j { | ||
continue | ||
} | ||
|
||
let distance = (anotherpoint[0] - point[0]) * (anotherpoint[0] - point[0]) + (anotherpoint[1] - point[1]) * (anotherpoint[1] - point[1]) | ||
|
||
if let sameDistancePoints = dict[distance] { | ||
dict[distance] = sameDistancePoints + 1 | ||
} else { | ||
dict[distance] = 1 | ||
} | ||
} | ||
|
||
for key in dict.keys { | ||
num += dict[key]! * (dict[key]! - 1) | ||
} | ||
} | ||
return num | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters