Simple Java Solution based on hashmap 20 lines


  • 0
    D

    public class Solution {

        public int maxPoints(Point[] points) {
            if(points.length<=2) return points.length;
            int res=0;
            for(int i=0 ;i<points.length;i++){
                int samepos=0;int samexco=1;
                Map<Double,Integer> map=new HashMap<>();
                for(int j=0;j<points.length;j++){
                    if(i!=j){
                        if(points[i].x==points[j].x&&points[i].y==points[j].y){samepos++;}
                        if(points[i].x==points[j].x){samexco++;continue;}
                        double slope=(double)(points[i].y-points[j].y)/(double)(points[i].x-points[j].x);
                        if(map.containsKey(slope)){map.put(slope,map.get(slope)+1);}
                        else{map.put(slope,2);}
                        res=Math.max(res,map.get(slope)+samepos);
                    }
                }
                res=Math.max(res,samexco);
            }
            return res;
        }
    

    }


Log in to reply
 

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