-
Notifications
You must be signed in to change notification settings - Fork 889
/
NumberBoomerangs.swift
35 lines (30 loc) · 1.03 KB
/
NumberBoomerangs.swift
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
/**
* 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
}
}