File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 344
344
353|[ Design Snake Game] ( ./solutions/0353-design-snake-game.js ) |Medium|
345
345
354|[ Russian Doll Envelopes] ( ./solutions/0354-russian-doll-envelopes.js ) |Hard|
346
346
355|[ Design Twitter] ( ./solutions/0355-design-twitter.js ) |Medium|
347
+ 356|[ Line Reflection] ( ./solutions/0356-line-reflection.js ) |Medium|
347
348
357|[ Count Numbers with Unique Digits] ( ./solutions/0357-count-numbers-with-unique-digits.js ) |Medium|
348
349
363|[ Max Sum of Rectangle No Larger Than K] ( ./solutions/0363-max-sum-of-rectangle-no-larger-than-k.js ) |Hard|
349
350
365|[ Water and Jug Problem] ( ./solutions/0365-water-and-jug-problem.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments