-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLine-Reflection.java
More file actions
38 lines (28 loc) · 970 Bytes
/
Line-Reflection.java
File metadata and controls
38 lines (28 loc) · 970 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
356. Line Reflection
Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given set of points.
Example 1:
Given points = [[1,1],[-1,1]], return true.
Example 2:
Given points = [[1,1],[-1,-1]], return false.
Follow up:
Could you do better than O(n2)?
Hint:
Find the smallest and largest x-value for all points.
If there is a line then it should be at y = (minX + maxX) / 2.
For each point, make sure that it has a reflected point in the opposite side.
public boolean isReflected(int[][] points) {
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
Set<String> set = new HashSet<String>();
for (int[] p : points) {
set.add(p[0] + "," + p[1]);
min = Math.min(min, p[0]);
max = Math.max(max, p[0]);
}
int sum = min + max;
for (int[] p : points) {
if (!set.contains((sum - p[0]) + "," + p[1])) {
return false;
}
}
return true;
}