/149. Max Points on a Line

149. Max Points on a Line

Hard
Arrays30.4% acceptance

Given a list of 2D coordinates, represented as integer pairs, determine the maximum number of points that are collinear (lie on the same straight line). Return this maximum count.

Example 1

Input: [[0,0],[1,2],[2,4],[3,6]]

Output: 4

Explanation: All points lie on the line y = 2x.

Example 2

Input: [[2,3],[4,7],[6,11],[8,15],[10,19],[1,1]]

Output: 5

Explanation: Five points lie on the line y = 2x + -1.

Example 3

Input: [[0,1],[1,0],[2,2],[3,3]]

Output: 2

Explanation: No three points are collinear; maximum is 2.

Constraints

  • 1 <= len(coordinates) <= 300
  • len(coordinates[i]) == 2 for all i
  • -104 <= coordinates[i][0], coordinates[i][1] <= 104
  • All coordinates are unique
Python (current runtime)

Case 1

Input: [[1,5],[2,10],[3,15],[4,20],[5,25]]

Expected: 5

Case 2

Input: [[0,0],[1,1],[2,2],[3,3],[4,5]]

Expected: 4

Case 3

Input: [[1,1],[2,2],[3,3],[4,4],[5,6],[7,8]]

Expected: 4

Case 4

Input: [[10,10],[20,20],[30,30],[40,40],[50,50],[60,61]]

Expected: 5

Case 5

Input: [[0,0],[1,2],[2,1],[3,3],[4,4],[5,5]]

Expected: 3