> ## 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.

# Minimum Area Rectangle Python Solution

> Tested Python solution for LeetCode 939 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 939, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Math](/catalog/topics/math), [Geometry](/catalog/topics/geometry), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/minimum-area-rectangle/description/).

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

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

## Problem

You are given an array of points in the **X-Y** plane `points` where `points[i] = [x<sub>i</sub>, y<sub>i</sub>]`.

Return *the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes*. If there is not any such rectangle, return `0`.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/08/03/rec1.JPG)

```
Input: points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Explanation: The minimum area rectangle is shown in the image, with an area of 2 * 2 = 4.
```

![Example 2](https://assets.leetcode.com/uploads/2021/08/03/rec2.JPG)

```
Input: points = [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Explanation: The minimum area rectangle is shown in the image, with an area of 1 * 2 = 2.
```

### Constraints

* 1 \<= points.length \<= 500
* points\[i].length == 2
* 0 \<= xi, yi \<= 4 \* 10^4
* All the given points are unique.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(sum k_x^2) where k_x = points sharing the same x, at most O(n^2)
    # Space: O(n)
    def min_area_rect(self, points: list[list[int]]) -> int:
        columns: dict[int, list[int]] = {}
        for x, y in points:
            columns.setdefault(x, []).append(y)

        last_x: dict[tuple[int, int], int] = {}
        result = 0
        for x in sorted(columns):
            ys = sorted(columns[x])
            for i, y1 in enumerate(ys):
                for y2 in ys[i + 1 :]:
                    pair = (y1, y2)
                    prev_x = last_x.get(pair)
                    if prev_x is not None:
                        area = (x - prev_x) * (y2 - y1)
                        if result == 0 or area < result:
                            result = area
                    last_x[pair] = x
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(sum k\_x^2) where k\_x = points sharing the same x, at most O(n^2) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.