/**
* 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 Solution {
public int maxPoints(Point[] points) {
if (points == null) return 0;
if (points.length <= 2) return points.length;
HashMap<Integer, HashMap<Integer, Integer>> map = new HashMap<>();
int max = 0;
for (int i = 0; i < points.length; i++) {
map.clear();
int dup = 0, count = 0;
for (int j = i + 1; j < points.length; j++) {
int x = points[j].x - points[i].x;
int y = points[j].y - points[i].y;
if (x == 0 && y == 0) {
dup++;
continue;
}
int gcd = calculateGCD(x, y);
if (gcd != 0) {
x /= gcd;
y /= gcd;
}
if (map.containsKey(x)) {
if (map.get(x).containsKey(y)) {
map.get(x).put(y, map.get(x).get(y) + 1);
} else {
map.get(x).put(y, 1);
}
} else {
HashMap<Integer, Integer> innerMap = new HashMap<>();
innerMap.put(y, 1);
map.put(x, innerMap);
}
count = Math.max(count, map.get(x).get(y));
}
max = Math.max(max, count + dup + 1);
}
return max;
}
private int calculateGCD(int a, int b) {
if (b == 0) return a;
return calculateGCD(b, a % b);
}
}