Maximum Points on a Line

Hard Topics Company Tags

Each pair [x, y] in points gives a distinct point in the coordinate plane. Return the largest number of these points that lie on one straight line. Lines may be horizontal, vertical, or slanted; points need not be evenly spaced.

The drawn line connects a largest collinear group of points.

Example 1:

Max Points on a Line, example 1: illustration of the example

Input: points = [[2,0],[2,4],[2,-3],[5,1]]

Output: 3

Explanation: Three points have x-coordinate 2, so they lie on the vertical line x = 2. The fourth point is outside that line.

Example 2:

Max Points on a Line, example 2: illustration of the example

Input: points = [[0,1],[2,1],[5,1],[1,4]]

Output: 3

Explanation: Three points have y-coordinate 1, placing them on the horizontal line y = 1. The remaining point is not on it.

Example 3:

Max Points on a Line, example 3: illustration of the example

Input: points = [[0,0],[1,2],[3,6],[2,3]]

Output: 3

Explanation: The points (0,0), (1,2), and (3,6) satisfy y = 2x. Their spacing differs, but all three lie on the same line; (2,3) does not.

Constraints:

  • 1 <= points.length <= 300
  • Each point contains exactly two integer coordinates.
  • -10000 <= points[i][j] <= 10000
  • All points are distinct.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.