We need to find the maximum number of points that lie on the same straight line.
For each point, treat it as an anchor and compute slopes formed with all other points.
Points that share the same slope with respect to the anchor lie on the same line.
Special cases must also be handled:
- Duplicate points
- Vertical lines (undefined slope)
- For each anchor point
i, use a hash map to count all slopes with other points. samecounts all points identical to the anchor.same_ycounts points with the same y-coordinate.- General case:
- Compute
dxanddy, usedx / dy(double) as the slope key.
- Compute
- After finishing all comparisons for point
i, update the global maximum. - Clear the hash map before moving to the next anchor.
For each point, other points define different "directions" (slopes).
- Same slope → lie on the same straight line
- Duplicate points → should be added to every slope count
- Horizontal lines → captured by
same_y
This converts the geometry problem into counting frequency of slopes.
- Time:
O(n²) - Space:
O(n)