javascript solution


  • 1
    J
     * Definition for a point.
     * function Point(x, y) {
     *     this.x = x;
     *     this.y = y;
     * }
     */
    /**
     * @param {Point[]} points
     * @return {number}
     */
    var maxPoints = function(points) {
          var max = 0;
    
        var len = points.length;
        if(len==1) return 1;
    
        for(let i=0;i<len;i++){
            var map= {};
            var tmp =0, nSame = 0, nInf = 0;
    
            for(let j=i+1;j<len;j++){
    
                if(points[i].x===points[j].x){
                    if(points[i].y===points[j].y){
                    nSame++;
                    }else {
                    nInf++;
                    }
                }else{
                 var ratio = (points[j].y - points[i].y)/(points[j].x - points[i].x);
                 if(map[ratio]===undefined){
                     map[ratio] = 1;
                 }else{
                     map[ratio]++;
                 }
                 tmp = Math.max(tmp,map[ratio]);
                }
         
            }
    
                tmp = Math.max(tmp, nInf)+nSame+1;
                max = Math.max(max,tmp);
            
        }
    
    
        return max;
    };

Log in to reply
 

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