Skip to content

Commit

Permalink
Fix issue in MaxPointsOnALine
Browse files Browse the repository at this point in the history
Record both slope and intercept to identify a line.
  • Loading branch information
mengli committed May 24, 2015
1 parent 7af17b5 commit 82334b9
Showing 1 changed file with 66 additions and 21 deletions.
87 changes: 66 additions & 21 deletions MaxPointsOnALine.java
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)}));
}
}

0 comments on commit 82334b9

Please sign in to comment.