LeetCode 149 Max Points on Line - Why Naive Fails

Why brute force fails

Pick every pair of points. For each pair, count how many other points lie on that line.

There are O(n2)O(n^2) pairs. For each pair, checking all other points is O(n)O(n). Total: O(n3)O(n^3).

With n=300n = 300, that's 3003=27300^3 = 27 million operations. Too slow.

You need a way to avoid rechecking the same line from different pairs.