From 53b1b1d88d6abdc83b5183085525c03fe7ef8aea Mon Sep 17 00:00:00 2001 From: Yi Gu Date: Sun, 2 Apr 2017 00:05:34 -0700 Subject: [PATCH] Add Solution to Number of Boomerangs --- Array/NumberBoomerangs.swift | 35 +++++++++++++++++++++++++++++++++++ README.md | 3 ++- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 Array/NumberBoomerangs.swift diff --git a/Array/NumberBoomerangs.swift b/Array/NumberBoomerangs.swift new file mode 100644 index 00000000..4eaab040 --- /dev/null +++ b/Array/NumberBoomerangs.swift @@ -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 + } +} \ No newline at end of file diff --git a/README.md b/README.md index 75b881c6..86e3e608 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ * [Microsoft](#microsoft) ## Progress -[Problem Status](#problem-status) shows the latest progress to all 500+ questions. Currently we have 217 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys). +[Problem Status](#problem-status) shows the latest progress to all 500+ questions. Currently we have 218 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys). ## Array @@ -35,6 +35,7 @@ | ----- | -------- | ---------- | ---- | ----- | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/)| [Swift](./Array/MaxConsecutiveOnes.swift)| Easy| O(n)| O(1)| [Heaters](https://leetcode.com/problems/heaters/)| [Swift](./Array/Heaters.swift)| Easy| O(nlogn)| O(1)| +[Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/)| [Swift](./Array/NumberBoomerangs.swift)| Easy| O(n ^ 2)| O(n)| [Island Perimeter](https://leetcode.com/problems/island-perimeter/)| [Swift](./Array/IslandPerimeter.swift)| Easy| O(nm)| O(1)| [Majority Element](https://leetcode.com/problems/majority-element/)| [Swift](./Array/MajorityElement.swift)| Easy| O(n)| O(1)| [Majority Element II](https://leetcode.com/problems/majority-element-ii/)| [Swift](./Array/MajorityElementII.swift)| Medium| O(n)| O(1)|