LeetCode 0356. Line Reflection Solution in Java, Python, C++, JavaScript, Go & Rust | Explanation + Code

CoderIndeed
0
0356. Line Reflection

Description

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.

 

Example 1:

Input: points = [[1,1],[-1,1]]
Output: true
Explanation: We can choose the line x = 0.

Example 2:

Input: points = [[1,1],[-1,-1]]
Output: false
Explanation: We can't choose a line.

 

Constraints:

  • n == points.length
  • 1 <= n <= 104
  • -108 <= points[i][j] <= 108

 

Follow up: Could you do better than O(n2)?

Solutions

Solution 1

PythonJavaC++Go
class Solution: def isReflected(self, points: List[List[int]]) -> bool: min_x, max_x = inf, -inf point_set = set() for x, y in points: min_x = min(min_x, x) max_x = max(max_x, x) point_set.add((x, y)) s = min_x + max_x return all((s - x, y) in point_set for x, y in points)(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !