Advertisement

Max Points on a Line - LeetCode 149 Solution

Max Points on a Line - Complete Solution Guide

Max Points on a Line is LeetCode problem 149, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an array of points where points[i] = [x i , y i ] 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 <= 300 points[i].length == 2 -10 4 <= x i , y i <= 10 4 All the points are unique .

Detailed Explanation

The problem asks us to find the maximum number of points that lie on the same straight line, given a set of points in a 2D plane. The input is an array of points, where each point is represented as a pair of coordinates [x, y]. The output is an integer representing the maximum number of points that lie on the same line. The constraints are that the number of points can range from 1 to 300, the coordinates are within -10000 to 10000, and all points are unique.

Solution Approach

The provided solution iterates through each point in the input array. For each point, it calculates the slope between that point and all other points. It stores the slopes in a hash map (dictionary in Python, HashMap in Java, unordered_map in C) where the key is the slope (or a representation of the slope, like a tuple of (dy, dx) after GCD reduction), and the value is the number of points with that slope. After iterating through all other points, it finds the maximum number of points that have the same slope. Finally, it updates the overall maximum number of points found so far.

Step-by-Step Algorithm

  1. Step 1: Iterate through each point `i` in the input array `points` from index 0 to n-1.
  2. Step 2: For each point `i`, initialize a hash map `slopes` to store the counts of slopes between `points[i]` and other points.
  3. Step 3: Iterate through the remaining points `j` from `i+1` to n-1.
  4. Step 4: Calculate the difference in x-coordinates `dx` and y-coordinates `dy` between `points[i]` and `points[j]`.
  5. Step 5: If `dx` is 0, the slope is infinite (vertical line). Store this specially, either using `float('inf')` or a special string or an agreed upon value in the hashmap.
  6. Step 6: Otherwise, calculate the greatest common divisor (GCD) of `dx` and `dy`. Simplify the slope by dividing `dx` and `dy` by their GCD. Store the simplified slope (e.g., as a tuple (dy, dx)) as the key in the `slopes` hash map, and increment the count for that slope.
  7. Step 7: After iterating through all other points `j`, find the maximum value among the counts in the `slopes` hash map. This represents the maximum number of points that lie on a line with `points[i]` (excluding the initial point `i`).
  8. Step 8: Update the overall maximum number of points (`max_points`) by taking the maximum between the current `max_points` and `current_max + 1` (to include the point `i` itself).
  9. Step 9: After iterating through all points `i`, return the final `max_points`.

Key Insights

  • Insight 1: The core idea is to iterate through each point and calculate the slope between that point and every other point. Points with the same slope from a given point lie on the same line.
  • Insight 2: We need to handle the case where the slope is infinite (vertical lines), which occurs when the x-coordinates of two points are equal. Using `float('inf')` or a special string like "Infinity" or storing the slope as a reduced fraction (dy/dx) are common solutions.
  • Insight 3: Storing the slopes as reduced fractions (dy/dx) with the greatest common divisor (GCD) allows for accurate comparison of slopes, avoiding floating-point precision issues that could arise when using doubles for representing slopes directly.
  • Insight 4: The problem can be solved with O(n^2) time complexity. The maximum number of points on a line can be determined by considering each point and then checking all other points.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Math, Geometry.

Companies

Asked at: Cisco, Citadel, LinkedIn, Nvidia, Oracle, Sprinklr, Waymo, X.