/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
class Solution {
public int maxPoints(Point[] points) {
if(points == null || points.length == 0){
return 0;
}
if(points.length <= 2)
return points.length;
HashMap<Integer,HashMap<Integer,Integer>> map = new HashMap<Integer,HashMap<Integer,Integer>>();
int result = 0;
for(int i = 0; i < points.length; i++){
map.clear();
int overlap = 0, max = 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){
overlap++;
continue;
}
int gcd = getGCD(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> subMap = new HashMap<>();
subMap.put(y,1);
map.put(x,subMap);
}
max = Math.max(max,map.get(x).get(y));
}
result = Math.max(result,max+overlap+1);
}
return result;
}
public int getGCD(int x,int y){
if(y == 0)
return x;
else return getGCD(y,x%y);
}
}