hardMath / GeometryPure DSA~38 min
Most Sensors on a Single Straight Line
Sensors are placed at integer coordinates on a floor plan. To validate a calibration rail, find the largest number of sensors that lie on one straight line.
Problem
Given an array points where points[i] = [xi, yi], return the maximum number of points that lie on the same straight line.
Input
An array points of length [1, 300]; each point [x, y] with coordinates in [-10^4, 10^4]. Points may repeat.
Output
An integer: the maximum number of collinear points.
Constraints
- 1 <= points.length <= 300
- -10^4 <= xi, yi <= 10^4
- All points are distinct unless explicitly duplicated.
Examples
Example 1
Input
[[1,1],[2,2],[3,3]]
Output
3
All three lie on y = x.
Example 2
Input
[[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output
4
Four points share one line.