Talentd
FleetCode

356. Line Reflection

Medium36.3% acceptance2 companiesPremium elsewhere, free here

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)?

Asked at 2 companies

Hints open one at a time

Hint 1
Find the smallest and largest x-value for all points.
Hint 2
If there is a line then it should be at y = (minX + maxX) / 2.
Hint 3
For each point, make sure that it has a reflected point in the opposite side.
18:33
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
[[1,1],[-1,1]]
Input
[[1,1],[-1,-1]]
Your code is saved on this device.Solution