Max Points on a Line

Hard#149counting

You get points on a plane, no two the same. Find the largest number of them that lie on one straight line. Our example: (1, 1), (2, 3), (4, 2), (3, 5), (0, 4), (5, 9) — the answer is 4: (1, 1), (2, 3), (3, 5) and (5, 9) lie on one line.

input
points: an array of points on a plane, each [x, y] with integer coordinates; no two points are the same.
returns
The largest number of the points that lie on one straight line.
constraints
  • 1 ≤ points.length ≤ 300
  • −10⁴ ≤ xᵢ, yᵢ ≤ 10⁴
  • all the points are different
our 13 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[][]} points
 * @return {number}
 */
function maxPoints(points) {

}
Ctrl ↵
format
reset

saved in this browser

13 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more hash map problems

Pattern “Hash map” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.