loading

Max Points on a Line — Step-by-Step Visualization

hardLeetCode #149ArrayHash TableMathGeometry

Return maximum number of points on the same line.

Algorithm Pattern

Slope Hashing per Anchor

Key Idea

For each anchor, hash normalized slopes to other points. Max frequency + 1 = max collinear points through anchor.

Step-by-Step Approach

  1. For each point i as anchor
  2. For each j != i, compute dy=y2-y1, dx=x2-x1
  3. Normalize by gcd, handle vertical/horizontal
  4. Count frequency of each slope
  5. Track global max

Related Problems