Skip to content

Commit f16cf7c

Browse files
committed
Add solution #356
1 parent e6f42ed commit f16cf7c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@
344344
353|[Design Snake Game](./solutions/0353-design-snake-game.js)|Medium|
345345
354|[Russian Doll Envelopes](./solutions/0354-russian-doll-envelopes.js)|Hard|
346346
355|[Design Twitter](./solutions/0355-design-twitter.js)|Medium|
347+
356|[Line Reflection](./solutions/0356-line-reflection.js)|Medium|
347348
357|[Count Numbers with Unique Digits](./solutions/0357-count-numbers-with-unique-digits.js)|Medium|
348349
363|[Max Sum of Rectangle No Larger Than K](./solutions/0363-max-sum-of-rectangle-no-larger-than-k.js)|Hard|
349350
365|[Water and Jug Problem](./solutions/0365-water-and-jug-problem.js)|Medium|

solutions/0356-line-reflection.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 356. Line Reflection
3+
* https://leetcode.com/problems/line-reflection/
4+
* Difficulty: Medium
5+
*
6+
* Given n points on a 2D plane, find if there is such a line parallel to the y-axis that reflects
7+
* the given points symmetrically.
8+
*
9+
* In other words, answer whether or not if there exists a line that after reflecting all points
10+
* over the given line, the original points' set is the same as the reflected ones.
11+
*
12+
* Note that there can be repeated points.
13+
*/
14+
15+
/**
16+
* @param {number[][]} points
17+
* @return {boolean}
18+
*/
19+
var isReflected = function(points) {
20+
const pointSet = new Set(points.map(([x, y]) => `${x},${y}`));
21+
let minX = Infinity;
22+
let maxX = -Infinity;
23+
24+
for (const [x] of points) {
25+
minX = Math.min(minX, x);
26+
maxX = Math.max(maxX, x);
27+
}
28+
29+
const sum = minX + maxX;
30+
for (const [x, y] of points) {
31+
if (!pointSet.has(`${sum - x},${y}`)) {
32+
return false;
33+
}
34+
}
35+
36+
return true;
37+
};

0 commit comments

Comments
 (0)