|
1 | 1 |
|
2 | 2 |
|
3 | 3 | /** |
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. |
5 | 6 | */ |
6 | 7 |
|
7 | 8 | import java.util.HashMap; |
8 | 9 | import java.util.Map; |
9 | 10 |
|
| 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 | + */ |
10 | 20 | 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 | + } |
11 | 54 | public int maxPoints(Point[] points) { |
12 | | - Map<Double, Integer> map = new HashMap<Double, Integer>(); |
| 55 | + Map<Line, Integer> map = new HashMap<Line, Integer>(); |
13 | 56 | int ret = 0; |
14 | 57 | int size = points.length; |
15 | 58 | for (int i = 0; i < size; i++) { |
16 | | - int invalidK = 0; |
17 | | - int add = 1; |
| 59 | + int dup = 0; |
18 | 60 | for (int j = i + 1; j < size; j++) { |
| 61 | + Line line = null; |
19 | 62 | if (points[j].x == points[i].x) { |
20 | 63 | if (points[j].y == points[i].y) { |
21 | | - add++; |
22 | | - } else { |
23 | | - invalidK++; |
| 64 | + dup++; |
| 65 | + continue; |
24 | 66 | } |
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); |
26 | 74 | } |
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); |
33 | 77 | } else { |
34 | | - map.put(k, 1); |
| 78 | + map.put(line, 2); |
35 | 79 | } |
36 | 80 | } |
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); |
41 | 83 | } |
42 | | - ret = Math.max(invalidK + add, ret); |
43 | 84 | map.clear(); |
44 | 85 | } |
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)})); |
46 | 91 | } |
47 | 92 | } |
0 commit comments