> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Max Points on a Line Python Solution

> Tested Python solution for LeetCode 149 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 149, Hard. Topics: Array, Hash Table, Math, Geometry. [View on LeetCode](https://leetcode.com/problems/max-points-on-a-line/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 149   # by problem number
lcpy gen -s max_points_on_a_line   # by problem name
```

## Problem

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.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg)

```
Input: points = [[1,1],[2,2],[3,3]]
Output: 3
```

![Example 2](https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg)

```
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 \<= xi, yi \<= 10^4
* All the points are unique.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/max_points_on_a_line/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/max_points_on_a_line/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import math
from collections import defaultdict


class Solution:
    # Time: O(n^2) where n is the number of points
    # Space: O(n) for the hash map
    def max_points(self, points: list[list[int]]) -> int:
        if len(points) <= 2:
            return len(points)

        max_count = 0

        for i in range(len(points)):
            slope_count: defaultdict[tuple[int, int], int] = defaultdict(int)
            duplicate = 0
            current_max = 0

            x1, y1 = points[i]

            for j in range(i + 1, len(points)):
                x2, y2 = points[j]

                # Handle duplicate points
                if x1 == x2 and y1 == y2:
                    duplicate += 1
                    continue

                # Calculate slope as a reduced fraction (dx, dy)
                dx = x2 - x1
                dy = y2 - y1

                # Reduce to lowest terms using GCD
                gcd_val = math.gcd(dx, dy)
                if gcd_val != 0:
                    dx //= gcd_val
                    dy //= gcd_val

                # Normalize the direction (ensure consistent representation)
                if dx < 0:
                    dx = -dx
                    dy = -dy
                elif dx == 0:
                    dy = abs(dy)

                slope = (dx, dy)
                slope_count[slope] += 1
                current_max = max(current_max, slope_count[slope])

            max_count = max(max_count, current_max + duplicate + 1)

        return max_count
```

## Complexity

| Time                                   | Space                 |
| -------------------------------------- | --------------------- |
| O(n^2) where n is the number of points | O(n) for the hash map |

## Tags

[NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
