-
Notifications
You must be signed in to change notification settings - Fork 45
/
_356.java
34 lines (32 loc) · 1.16 KB
/
_356.java
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
import java.util.Arrays;
/**
* LeetCode 356 - Line Reflection
*
* In-place solution
*/
public class _356 {
public boolean isReflected(int[][] points) {
Arrays.sort(points, (u, v) -> {
if (u[1] != v[1]) return Integer.compare(u[1], v[1]);
else return Integer.compare(u[0], v[0]);
});
int sum = 0, n = points.length;
boolean hasSum = false;
for (int i = 0; i < points.length; ) {
int j = i;
while (j + 1 < points.length && points[i][1] == points[j + 1][1]) j++;
int left = i, right = j;
while (left <= right) { // has to use <=, otherwise single element will be missed.
while (left < right && points[left][0] == points[left + 1][0]) left++;
while (left < right && points[right][0] == points[right - 1][0]) right--;
if (left <= right) {
if (hasSum && points[left][0] + points[right][0] != sum) return false;
sum = points[left++][0] + points[right--][0];
hasSum = true;
}
}
i = j + 1;
}
return true;
}
}