bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 149. Max Points on a Line

149. Max Points on a Line

Leetcode Solutions

Key approach of the solution.

  1. Initialize a variable max_points to keep track of the maximum number of points on the same line.
  2. Iterate over each point points[i] in the array.
    • Initialize a hash map slope_count to store the count of points with the same slope relative to points[i].
    • For each other point points[j], where j != i, calculate the slope.
    • Normalize the slope to handle infinite slopes and precision issues.
    • Increment the count of the slope in slope_count.
    • Update max_points with the maximum value from slope_count plus one (to include points[i]).
  3. Return max_points as the result.
UML Thumbnail

Alternative approach using cross product

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR