Simple JavaScript solution


  • 0
    L
    function maxPoints(points) {
        return points.reduce((max, p, i) => {
            var map = {POSITIVE_INFINITY: 0}; // exclude point[i]
            var samePoint = 1; // include point[i]
            
            for (var j = i + 1; j < points.length; j++) {
                var p2 = points[j];
                
                if (p2.x === p.x && p2.y === p.y) {
                    samePoint++;
                } else {
                    var slope = p2.x === p.x ? Number.POSITIVE_INFINITY : (p2.y - p.y) / (p2.x - p.x);
                    map[slope] = (map[slope] || 0) + 1;
                }
            }
            return Math.max(max, ...Object.keys(map).map(k => map[k] + samePoint));
        }, 0);
    }

Log in to reply
 

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