For each anchor point, count slopes to all others using a hash map. Track the maximum count.
function maxPoints(points): if len(points) <= 2: return len(points) result = 0 for i in range(len(points)): slopes = {} for j in range(len(points)): if i == j: continue slope = getSlope(points[i], points[j]) slopes[slope] = slopes.get(slope, 0) + 1 result = max(result, max(slopes.values()) + 1) return result
function getSlope(p1, p2): dx = p2[0] - p1[0] dy = p2[1] - p1[1] g = gcd(abs(dx), abs(dy)) return (dy // g, dx // g)
time, space.