#149

Max Points on a Line

Hard HashMap of reduced slopes

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Given points on a plane, return the largest number of them that lie on one straight line.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the most points on one straight line.

Why this works: fix one point; points on the same line through it share a slope. Store slopes as reduced (dy, dx) fractions to avoid decimals.

Example: [[1,1],[2,2],[3,3]]

from (1,1): (1,1) slope twice → 2 + 1 = 3 points

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Using double slopes — precision errors, and −0.0 vs 0.0 are different keys.

Solve on LeetCode ↗
Complexity
  • Time: O(n² log V) for the GCDs.
  • Space: O(n).
Topic: Hashing
🎬 Episodes 37–39: video coming soon