Problem Number: 149 Difficulty: Hard Category: Array, Hash Table, Math, Geometry LeetCode Link: https://leetcode.com/problems/max-points-on-a-line/
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
Explanation: All points lie on the line y = x.
Example 2:
Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4
Explanation: The line through points [1,1], [4,1] contains 4 points.
Constraints:
1 <= points.length <= 300points[i].length == 2-10^4 <= xi, yi <= 10^4- All the points are unique.
I used a Hash Table approach with slope calculation. The key insight is to calculate the slope between each pair of points and count how many points share the same slope from a fixed reference point.
Algorithm:
- Handle edge cases (≤ 2 points return length)
- For each point as reference, calculate slopes to all other points
- Use hash table to count points with same slope
- Handle vertical lines (infinite slope) separately
- Track maximum points found on any line
- Return maximum count + 1 (including reference point)
The solution uses hash table with slope calculation to find maximum points on a line. See the implementation in the solution file.
Key Points:
- Uses hash table to count points with same slope
- Handles vertical lines (infinite slope) with special case
- Calculates slope using (y2-y1)/(x2-x1) formula
- Tracks maximum points found for each reference point
- Returns maximum count including reference point
Time Complexity: O(n²)
- For each point, check all other points
- Slope calculation is constant time
- Total: O(n²)
Space Complexity: O(n)
- Hash table stores at most n-1 slopes
- Each slope can have multiple points
- Total: O(n)
-
Slope as Key: Using slope as hash table key groups points on same line.
-
Reference Point: For each reference point, we calculate slopes to all other points.
-
Vertical Lines: Vertical lines have infinite slope and need special handling.
-
Duplicate Points: The problem guarantees unique points, simplifying the logic.
-
Maximum Tracking: We track the maximum points found for each reference point.
-
Slope Formula: Using (y2-y1)/(x2-x1) to calculate slope between two points.
-
Floating Point Precision: Using floating point for slope comparison can lead to precision issues.
-
Vertical Line Handling: Not properly handling vertical lines with infinite slope.
-
Reference Point Logic: Not understanding that we need to check each point as reference.
-
Complex Implementation: Overcomplicating the slope calculation and counting.
- Line Reflection (Problem 356): Check if points are symmetric about a line
- Convex Hull (Problem 587): Find convex hull of points
- Rectangle Area (Problem 223): Calculate area of overlapping rectangles
- Valid Square (Problem 593): Check if four points form a square
- GCD for Slope: Use GCD to represent slope as reduced fraction - O(n²) time
- Cross Product: Use cross product to check collinearity - O(n²) time
- Brute Force: Check all possible lines through point pairs - O(n³) time
- Floating Point Issues: Using floating point for slope comparison.
- Vertical Line Handling: Not properly handling infinite slopes.
- Reference Point Logic: Not checking each point as potential reference.
- Complex Implementation: Overcomplicating the slope calculation.
- Edge Cases: Not handling cases with ≤ 2 points properly.
Note: This is a geometry problem that demonstrates efficient slope-based line detection with hash tables.