-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0356_line_reflection.go
More file actions
46 lines (37 loc) · 995 Bytes
/
s0356_line_reflection.go
File metadata and controls
46 lines (37 loc) · 995 Bytes
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
36
37
38
39
40
41
42
43
44
45
46
/*
https://leetcode.com/problems/line-reflection/description/
Given n points on a 2D plane, find if there is such a line parallel to the
y-axis that reflects the given points
symmetrically.
In other words, answer whether or not if there exists a line that after
reflecting all points over the given line,
the original points' set is the same as the reflected ones.
Note that there can be repeated points.
*/
package solutions
import "math"
func isReflected(points [][]int) bool {
minX, maxX := math.MaxInt, math.MinInt
ps := make(map[[2]int]bool)
for _, p := range points {
ps[[2]int{p[0], p[1]}] = true
minX = min(minX, p[0])
maxX = max(maxX, p[0])
}
x := float64(minX+maxX) / float64(2)
for _, p := range points {
var mirror float64
px := float64(p[0])
if px < x {
mirror = px + (x-px)*2
} else if px > x {
mirror = px - (px-x)*2
} else {
mirror = x
}
if _, ok := ps[[2]int{int(mirror), p[1]}]; !ok {
return false
}
}
return true
}