-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0356-line-reflection.js
More file actions
37 lines (33 loc) · 929 Bytes
/
0356-line-reflection.js
File metadata and controls
37 lines (33 loc) · 929 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
/**
* 356. Line Reflection
* https://leetcode.com/problems/line-reflection/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {number[][]} points
* @return {boolean}
*/
var isReflected = function(points) {
const pointSet = new Set(points.map(([x, y]) => `${x},${y}`));
let minX = Infinity;
let maxX = -Infinity;
for (const [x] of points) {
minX = Math.min(minX, x);
maxX = Math.max(maxX, x);
}
const sum = minX + maxX;
for (const [x, y] of points) {
if (!pointSet.has(`${sum - x},${y}`)) {
return false;
}
}
return true;
};