Line Reflection — LeetCode 356 Python Solution
- Problem
- #356
- Pattern
- Math and Number Theory
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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.
Example
- Input
- points = [[1,1],[-1,1]]
- Output
- true
- Explanation
- We can choose the line x = 0.
Python solution
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)Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(n) auxiliary |
Pattern: Math and Number Theory
Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 356. Line Reflection is filed here because LeetCode tags it Math, which is the vocabulary this hub collects.
The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 356. Line Reflection?
- LeetCode 356. Line Reflection is rated Medium on LeetCode.
- What is the time complexity of LeetCode 356. Line Reflection?
- The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 356. Line Reflection?
- The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 356. Line Reflection cover?
- LeetCode 356. Line Reflection is tagged Array, Hash Table and Math on LeetCode.
- Is LeetCode 356. Line Reflection a premium problem?
- Yes. LeetCode 356. Line Reflection is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.