Simple Java Solution


  • 0
    public int maxPoints(Point[] points) {
            int l=points.length,result=0;
            if(l<=2) return l;
            for(int i=0;i<l;i++){
                HashMap<Double,Integer> map=new HashMap<>();
                int samex=0,samep=0,r=0;
                for(int j=0;j<l;j++){
                    if((points[j].x==points[i].x)&&(points[j].y==points[i].y)){
                        samep++;
                    }else if(points[j].x==points[i].x){
                        samex++;
                    }else{
                        double k=(double)(points[j].y-points[i].y)/(double)(points[j].x-points[i].x);
                        int count=map.getOrDefault(k,0);
                        map.put(k,count+1);
                        r=Math.max(r,count+1);
                    }
                }
                r=Math.max(r+samep,samex+samep);
                result=Math.max(r,result);
            }
            return result;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.