Skip to content

Commit

Permalink
[Math] Add a solution to Line Reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Dec 4, 2019
1 parent ff99daf commit d61910a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
33 changes: 33 additions & 0 deletions Math/LineReflection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Question Link: https://leetcode.com/problems/line-reflection/
* Primary idea: Find a Line that should be y = (minX + maxX) / 2, then iterate through points and make sure that it has a reflected point in the opposite side.
*
* Time Complexity: O(n), Space Complexity: O(n)
*/

class LineReflection {
func isReflected(_ points: [[Int]]) -> Bool {
var minX = Int.max, maxX = Int.min
var pointSet = Set<[Int]>()

for point in points {
pointSet.insert(point)
minX = min(point[0], minX)
maxX = max(point[0], maxX)
}

let sum = minX + maxX

for item in pointSet {
if item[0] == sum {
continue
}

if !pointSet.contains([sum - item[0], item[1]]) {
return false
}
}

return true
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Leetcode](./logo.png?style=centerme)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 297 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 298 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.

## Contributors

Expand Down Expand Up @@ -327,6 +327,7 @@
[K-th Smallest in Lexicographical Order](https://leetcode.com/problems/k-th-smallest-in-lexicographical-order/)| [Swift](./Math/KthSmallestLexicographicalOrder.swift)| Hard| O(n)| O(1)|
[Gary Code](https://leetcode.com/problems/gray-code/)| [Swift](./Math/GaryCode.swift)| Medium| O(n)| O(2^n)|
[Permutation Sequence](https://leetcode.com/problems/permutation-sequence/)| [Swift](./Math/PermutationSequence.swift)| Medium| O(n^2)| O(1)|
[Line Reflection](https://leetcode.com/problems/line-reflection/)| [Swift](./Math/LineReflection.swift)| Medium| O(n)| O(n)|

## Search
| Title | Solution | Difficulty | Time | Space |
Expand Down

0 comments on commit d61910a

Please sign in to comment.