> ## 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 II Python Solution

> Tested Python solution for LeetCode 963 with 22 pytest cases. Generate a practice environment with lcpy.

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

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

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 963   # by problem number
lcpy gen -s minimum_area_rectangle_ii   # 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 any rectangle formed from these points, with sides **not necessarily parallel** to the X and Y axes*. If there is not any such rectangle, return `0`.

Answers within `10<sup>-5</sup>` of the actual answer will be accepted.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2018/12/21/1a.png)

```
Input: points = [[1,2],[2,1],[1,0],[0,1]]
Output: 2.00000
Explanation: The minimum area rectangle occurs at [1,2],[2,1],[1,0],[0,1], with an area of 2.
```

![Example 2](https://assets.leetcode.com/uploads/2018/12/22/2.png)

```
Input: points = [[0,1],[2,1],[1,1],[1,0],[2,0]]
Output: 1.00000
Explanation: The minimum area rectangle occurs at [1,0],[1,1],[2,1],[2,0], with an area of 1.
```

![Example 3](https://assets.leetcode.com/uploads/2018/12/22/3.png)

```
Input: points = [[0,3],[1,2],[3,1],[1,3],[2,1]]
Output: 0
Explanation: There is no possible rectangle to form from these points.
```

### Constraints

* 1 \<= points.length \<= 50
* 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_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_area_rectangle_ii/test_solution.py):

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


class Solution:
    # Time: O(n^2 + sum(g^2)) over diagonal groups
    # Space: O(n^2)
    def min_area_free_rect(self, points: list[list[int]]) -> float:
        pts = [complex(x, y) for x, y in points]
        groups: dict[tuple[complex, float], list[tuple[complex, complex]]] = defaultdict(list)
        for p1, p2 in combinations(pts, 2):
            center = (p1 + p2) / 2
            diag_sq = abs(p1 - p2) ** 2
            groups[(center, diag_sq)].append((p1, p2))
        best = float("inf")
        for diags in groups.values():
            for (p1, p2), (p3, _p4) in combinations(diags, 2):
                area = abs(p1 - p3) * abs(p2 - p3)
                best = min(best, area)
        return best if best != float("inf") else 0.0
```

## Complexity

| Time | Space |
| - | - |
| O(n^2 + sum(g^2)) over diagonal groups | O(n^2) |

## Tags


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