forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LineReflection.swift
33 lines (27 loc) · 916 Bytes
/
LineReflection.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
/**
* 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
}
}