LeetCode 356: Line Reflection Solution
Master LeetCode problem 356 (Line Reflection), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
356. Line Reflection
Problem Explanation
Explanation:
Given a list of points on a 2D plane, we need to determine if there exists a line such that the points can be reflected symmetrically about that line. To solve this problem, we can follow these steps:
- Calculate the minimum and maximum x-coordinates of the points to find the axis of symmetry.
- For each point, determine its reflection point on the axis of symmetry.
- Check if the reflection point exists in the set of points.
- If all points have valid reflection points, then return true; otherwise, return false.
Time complexity:
- Calculating the min and max x-coordinates: O(n)
- Creating a set of points: O(n)
- Checking if all points have valid reflections: O(n) Overall time complexity: O(n)
Space complexity:
- Storing the set of points: O(n)
:
Solution Code
import java.util.HashSet;
class Solution {
public boolean isReflected(int[][] points) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
HashSet<String> set = new HashSet<>();
for (int[] point : points) {
min = Math.min(min, point[0]);
max = Math.max(max, point[0]);
set.add(point[0] + "," + point[1]);
}
int sum = min + max;
for (int[] point : points) {
int reflectedX = sum - point[0];
if (!set.contains(reflectedX + "," + point[1])) {
return false;
}
}
return true;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 356 (Line Reflection)?
This page provides optimized solutions for LeetCode problem 356 (Line Reflection) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 356 (Line Reflection)?
The time complexity for LeetCode 356 (Line Reflection) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 356 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 356 in Java, C++, or Python.