-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Record both slope and intercept to identify a line.
- Loading branch information
Showing
1 changed file
with
66 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,92 @@ | ||
|
||
|
||
/** | ||
* Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. | ||
* Given n points on a 2D plane, find the maximum number of points | ||
* that lie on the same straight line. | ||
*/ | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Definition for a point. | ||
* class Point { | ||
* int x; | ||
* int y; | ||
* Point() { x = 0; y = 0; } | ||
* Point(int a, int b) { x = a; y = b; } | ||
* } | ||
*/ | ||
public class MaxPointsOnALine { | ||
|
||
private class Line { | ||
private final double mSlope; | ||
private final double mIntercept; | ||
|
||
public Line(double slope, double intercept) { | ||
mSlope = slope; | ||
mIntercept = intercept; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (this == object) { | ||
return true; | ||
} else if (object == null) { | ||
return false; | ||
} else if (object instanceof Line) { | ||
Line line = (Line) object; | ||
if (Math.abs(mSlope - line.mSlope) < 0.0001 | ||
&& Math.abs(mIntercept - line.mIntercept) < 0.0001) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
return prime * Double.toString(mSlope).hashCode() | ||
+ Double.toString(mIntercept).hashCode(); | ||
} | ||
} | ||
public int maxPoints(Point[] points) { | ||
Map<Double, Integer> map = new HashMap<Double, Integer>(); | ||
Map<Line, Integer> map = new HashMap<Line, Integer>(); | ||
int ret = 0; | ||
int size = points.length; | ||
for (int i = 0; i < size; i++) { | ||
int invalidK = 0; | ||
int add = 1; | ||
int dup = 0; | ||
for (int j = i + 1; j < size; j++) { | ||
Line line = null; | ||
if (points[j].x == points[i].x) { | ||
if (points[j].y == points[i].y) { | ||
add++; | ||
} else { | ||
invalidK++; | ||
dup++; | ||
continue; | ||
} | ||
continue; | ||
line = new Line(Double.MAX_VALUE, Double.MAX_VALUE); | ||
} else { | ||
double slope = points[j].y == points[i].y ? 0.0 | ||
: (1.0 * (points[j].y - points[i].y)) | ||
/ (points[j].x - points[i].x); | ||
double intercept = points[i].y - slope * points[i].x; | ||
line = new Line(slope, intercept); | ||
} | ||
double k = points[j].y == points[i].y ? 0.0 | ||
: (1.0 * (points[j].y - points[i].y)) | ||
/ (points[j].x - points[i].x); | ||
if (map.containsKey(k)) { | ||
int count = map.get(k); | ||
map.put(k, count + 1); | ||
if (map.containsKey(line)) { | ||
map.put(line, map.get(line) + 1); | ||
} else { | ||
map.put(k, 1); | ||
map.put(line, 2); | ||
} | ||
} | ||
for (Integer it : map.values()) { | ||
if (it + add > ret) { | ||
ret = it.intValue() + add; | ||
} | ||
for (Integer count : map.values()) { | ||
ret = Math.max(ret, count.intValue() + dup); | ||
} | ||
ret = Math.max(invalidK + add, ret); | ||
map.clear(); | ||
} | ||
return ret; | ||
return ret > 0 ? ret : points.length; | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println(new MaxPointsOnALine().maxPoints(new Point[] {new Point(0,0),new Point(0,0)})); | ||
} | ||
} |