/**
Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflects the given points.
Example 1:
Given points = [[1,1],[-1,1]], return true.
Example 2:
Given points = [[1,1],[-1,-1]], return false.
Follow up:
Could you do better than O(n2)?
*/
import java.util.*;
class Point{
double x, y;
Point(double x1, double y1){
x = x1;
y = y1;
}
public String toString(){
return "("+x+","+y+")";
}
}
public class LineReflection{
/**
-use hashmap to put x as key and the set of y-values as value
-in pass1, find the leftmost x value and rightmost x value
-define the mid value using the leftmost x and rightmost x value
-in pass2, for each point, check if the hashmap contains mid-x value or not, if contains, check if they have same y value, if either of condition is not satisfied return false
O(n), O(n)
*/
public static boolean checkReflection2(Point[] points){
if(points==null || points.length<1){
return true;
}
double lowx = Double.MAX_VALUE, highx = Double.MIN_VALUE;
HashMap<Double, HashSet<Double>> map = new HashMap<>();
for(Point p:points){
lowx = Math.min(lowx, p.x);
highx = Math.max(highx, p.x);
map.putIfAbsent(p.x, new HashSet<Double>());
map.get(p.x).add(p.y);
}
double midx = (lowx+highx);//we can store (low+high)/2 but this loses some value and wont find the same key when we search later
//now for each point, check if there is a reflection pair
for(Point p:points){
double refx = Math.abs(p.x - midx);
//if the reflection x with same y-value as p is not found, return false
if(!map.containsKey(refx) || !map.get(refx).contains(p.y)){
return false;
}
}
return true;
}
public static void main(String args[]){
Point[] points = new Point[7];
points[0] = new Point(1,1);
points[1] = new Point(1.5,1);
points[2] = new Point(2.5,1);
points[3] = new Point(3,1);
points[4] = new Point(4,2);
points[5] = new Point(0,2);
points[6] = new Point(0,3);
boolean ref1 = checkReflection1(points);
//boolean ref2 = checkReflection2(points);
System.out.println("ref is:"+ref1+"...and:"+ref2);
}
}
Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflects the given points.
Example 1:
Given points = [[1,1],[-1,1]], return true.
Example 2:
Given points = [[1,1],[-1,-1]], return false.
Follow up:
Could you do better than O(n2)?
*/
import java.util.*;
class Point{
double x, y;
Point(double x1, double y1){
x = x1;
y = y1;
}
public String toString(){
return "("+x+","+y+")";
}
}
public class LineReflection{
/**
-use hashmap to put x as key and the set of y-values as value
-in pass1, find the leftmost x value and rightmost x value
-define the mid value using the leftmost x and rightmost x value
-in pass2, for each point, check if the hashmap contains mid-x value or not, if contains, check if they have same y value, if either of condition is not satisfied return false
O(n), O(n)
*/
public static boolean checkReflection2(Point[] points){
if(points==null || points.length<1){
return true;
}
double lowx = Double.MAX_VALUE, highx = Double.MIN_VALUE;
HashMap<Double, HashSet<Double>> map = new HashMap<>();
for(Point p:points){
lowx = Math.min(lowx, p.x);
highx = Math.max(highx, p.x);
map.putIfAbsent(p.x, new HashSet<Double>());
map.get(p.x).add(p.y);
}
double midx = (lowx+highx);//we can store (low+high)/2 but this loses some value and wont find the same key when we search later
//now for each point, check if there is a reflection pair
for(Point p:points){
double refx = Math.abs(p.x - midx);
//if the reflection x with same y-value as p is not found, return false
if(!map.containsKey(refx) || !map.get(refx).contains(p.y)){
return false;
}
}
return true;
}
public static void main(String args[]){
Point[] points = new Point[7];
points[0] = new Point(1,1);
points[1] = new Point(1.5,1);
points[2] = new Point(2.5,1);
points[3] = new Point(3,1);
points[4] = new Point(4,2);
points[5] = new Point(0,2);
points[6] = new Point(0,3);
boolean ref1 = checkReflection1(points);
//boolean ref2 = checkReflection2(points);
System.out.println("ref is:"+ref1+"...and:"+ref2);
}
}
No comments:
Post a Comment