Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.
Example 1:
Input: points = [[1,1],[2,2],[3,3]] Output: 3
Example 2:
Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] Output: 4
Constraints:
1 <= points.length <= 300points[i].length == 2-104 <= xi, yi <= 104points are unique.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a handful of dots scattered across a large piece of paper and you want to find the straight line that captures the most dots. The brute force strategy works by systematically testing every possible pair of dots, drawing a straight path through them, and counting how many other dots happen to fall exactly on that same path.
Here's how the algorithm would work step-by-step:
def max_points(points): max_points_found = 1 total_points = len(points) if total_points <= 2: return total_points # Iterate through every possible starting dot to establish the baseline for our lines for first_index in range(total_points): first_point_x, first_point_y = points[first_index] for second_index in range(first_index + 1, total_points): second_point_x, second_point_y = points[second_index] current_line_count = 2 # Check every remaining dot to see if it collinear with our chosen pair for third_index in range(total_points): if third_index == first_index or third_index == second_index: continue third_point_x, third_point_y = points[third_index] if (second_point_y - first_point_y) * (third_point_x - second_point_x) == (third_point_y - second_point_y) * (second_point_x - first_point_x): current_line_count += 1 # Update our global maximum if the current line captures more dots than previous if current_line_count > max_points_found: max_points_found = current_line_count return max_points_foundInstead of comparing every possible combination of three or more points at once, we can solve this by focusing on one starting point at a time. By measuring the tilt of the path leading from that starting point to every other point, we can easily spot which destinations share the exact same straight line.
Here's how the algorithm would work step-by-step:
from collections import defaultdict
class Solution:
def maxPoints(self, points: list[list[int]]) -> int:
total_points = len(points)
if total_points <= 2:
return total_points
maximum_aligned_points = 1
# An anchor point is needed to measure relative slopes to all other points.
for anchor_index in range(total_points):
anchor_point = points[anchor_index]
slope_counts = defaultdict(int)
duplicates_count = 1
local_maximum = 0
for other_index in range(anchor_index + 1, total_points):
other_point = points[other_index]
if anchor_point[0] == other_point[0] and anchor_point[1] == other_point[1]:
duplicates_count += 1
continue
delta_x = other_point[0] - anchor_point[0]
delta_y = other_point[1] - anchor_point[1]
import math
greatest_common_divisor = math.gcd(delta_x, delta_y)
simplified_slope = (delta_x // greatest_common_divisor, delta_y // greatest_common_divisor)
slope_counts[simplified_slope] += 1
local_maximum = max(local_maximum, slope_counts[simplified_slope])
# Total points on this line include the anchor and overlapping duplicate coordinates.
maximum_aligned_points = max(maximum_aligned_points, local_maximum + duplicates_count)
return maximum_aligned_points| Case | How to Handle |
|---|---|
| Fewer than 3 points provided in the input array | Return the total number of points directly since 0, 1, or 2 points are always collinear. |
| Duplicate points with identical x and y coordinates | Track duplicates relative to the current anchor point and add them to every computed slope count. |
| Vertical lines where the change in x is zero | Represent the slope as an undefined or infinite value, such as using a string or a distinct tuple, to prevent division by zero errors. |
| Negative coordinates and mixed sign values | Normalize the greatest common divisor of the slope's numerator and denominator to always be positive so equivalent negative slopes match. |
| Floating point precision issues when calculating slopes | Use the greatest common divisor of differences in x and y to reduce slopes to irreducible fractions instead of using division. |
| Extremely large coordinate values causing integer overflow | Cast coordinate differences to 64-bit integers before computing products or greatest common divisors. |
| All points lying on the exact same horizontal line | The vertical difference remains zero while the horizontal difference varies, mapping all points to a slope of zero. |
| Massive inputs with thousands of points | Optimize the pairwise slope calculation using a nested loop with a hash map per outer iteration to achieve an efficient O(N^2) time complexity. |