Skip to content

Commit 82334b9

Browse files
committed
Fix issue in MaxPointsOnALine
Record both slope and intercept to identify a line.
1 parent 7af17b5 commit 82334b9

File tree

1 file changed

+66
-21
lines changed

1 file changed

+66
-21
lines changed

MaxPointsOnALine.java

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,92 @@
11

22

33
/**
4-
* Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
4+
* Given n points on a 2D plane, find the maximum number of points
5+
* that lie on the same straight line.
56
*/
67

78
import java.util.HashMap;
89
import java.util.Map;
910

11+
/**
12+
* Definition for a point.
13+
* class Point {
14+
* int x;
15+
* int y;
16+
* Point() { x = 0; y = 0; }
17+
* Point(int a, int b) { x = a; y = b; }
18+
* }
19+
*/
1020
public class MaxPointsOnALine {
21+
22+
private class Line {
23+
private final double mSlope;
24+
private final double mIntercept;
25+
26+
public Line(double slope, double intercept) {
27+
mSlope = slope;
28+
mIntercept = intercept;
29+
}
30+
31+
@Override
32+
public boolean equals(Object object) {
33+
if (this == object) {
34+
return true;
35+
} else if (object == null) {
36+
return false;
37+
} else if (object instanceof Line) {
38+
Line line = (Line) object;
39+
if (Math.abs(mSlope - line.mSlope) < 0.0001
40+
&& Math.abs(mIntercept - line.mIntercept) < 0.0001) {
41+
return true;
42+
}
43+
}
44+
return false;
45+
}
46+
47+
@Override
48+
public int hashCode() {
49+
final int prime = 31;
50+
return prime * Double.toString(mSlope).hashCode()
51+
+ Double.toString(mIntercept).hashCode();
52+
}
53+
}
1154
public int maxPoints(Point[] points) {
12-
Map<Double, Integer> map = new HashMap<Double, Integer>();
55+
Map<Line, Integer> map = new HashMap<Line, Integer>();
1356
int ret = 0;
1457
int size = points.length;
1558
for (int i = 0; i < size; i++) {
16-
int invalidK = 0;
17-
int add = 1;
59+
int dup = 0;
1860
for (int j = i + 1; j < size; j++) {
61+
Line line = null;
1962
if (points[j].x == points[i].x) {
2063
if (points[j].y == points[i].y) {
21-
add++;
22-
} else {
23-
invalidK++;
64+
dup++;
65+
continue;
2466
}
25-
continue;
67+
line = new Line(Double.MAX_VALUE, Double.MAX_VALUE);
68+
} else {
69+
double slope = points[j].y == points[i].y ? 0.0
70+
: (1.0 * (points[j].y - points[i].y))
71+
/ (points[j].x - points[i].x);
72+
double intercept = points[i].y - slope * points[i].x;
73+
line = new Line(slope, intercept);
2674
}
27-
double k = points[j].y == points[i].y ? 0.0
28-
: (1.0 * (points[j].y - points[i].y))
29-
/ (points[j].x - points[i].x);
30-
if (map.containsKey(k)) {
31-
int count = map.get(k);
32-
map.put(k, count + 1);
75+
if (map.containsKey(line)) {
76+
map.put(line, map.get(line) + 1);
3377
} else {
34-
map.put(k, 1);
78+
map.put(line, 2);
3579
}
3680
}
37-
for (Integer it : map.values()) {
38-
if (it + add > ret) {
39-
ret = it.intValue() + add;
40-
}
81+
for (Integer count : map.values()) {
82+
ret = Math.max(ret, count.intValue() + dup);
4183
}
42-
ret = Math.max(invalidK + add, ret);
4384
map.clear();
4485
}
45-
return ret;
86+
return ret > 0 ? ret : points.length;
87+
}
88+
89+
public static void main(String[] args) {
90+
System.out.println(new MaxPointsOnALine().maxPoints(new Point[] {new Point(0,0),new Point(0,0)}));
4691
}
4792
}

0 commit comments

Comments
 (0)