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

<p>Return <em>the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes</em>. If there is not any such rectangle, return <code>0</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/03/rec1.JPG" style="width: 500px; height: 447px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
<strong>Output:</strong> 4
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/03/rec2.JPG" style="width: 500px; height: 477px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
<strong>Output:</strong> 2
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= points.length &lt;= 500</code></li>
	<li><code>points[i].length == 2</code></li>
	<li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 4 * 10<sup>4</sup></code></li>
	<li>All the given points are <strong>unique</strong>.</li>
</ul>

class Solution:
    def minAreaRect(self, points: List[List[int]]) -> int:
        # make rectangles from one sides
        hor_sides = {}
        ver_sides = {}
        for p in points:
            x = p[0]
            y = p[1]
            if x in ver_sides:
                ver_sides[x].append(p)
            else:
                ver_sides[x] = [p]
            
            if y in hor_sides:
                hor_sides[y].append(p)
            else:
                hor_sides[y] = [p]
        for hors in hor_sides.values():
            hors.sort()
        for vers in ver_sides.values():
            vers.sort()
        keys = sorted(hor_sides.keys())
        min_area = 0
        for key in keys:
            hors = hor_sides[key]
            x_sides = []
            for i in range(len(hors)-1):
                for j in range(i+1, len(hors)):
                    x_sides.append([hors[i], hors[j]])
            if not x_sides:
                continue
            for x_side in x_sides:
                left = ver_sides[x_side[0][0]]
                right = ver_sides[x_side[1][0]]
                left_idx = 0
                right_idx = 0
                while left_idx < len(left) and right_idx < len(right):
                    if left[left_idx][1] <= x_side[0][1]:
                        left_idx +=1
                        continue
                    if right[right_idx][1] <= x_side[1][1]:
                        right_idx +=1
                        continue
                    if left[left_idx][1] == right[right_idx][1]:
                        area = (x_side[1][0] - x_side[0][0]) * (left[left_idx][1] - x_side[0][1])
                        if min_area == 0:
                            min_area = area
                        else:
                            min_area = min(min_area, area)
                        break
                    elif left[left_idx][1] < right[right_idx][1]:
                        left_idx += 1
                    else:
                        right_idx += 1
        return min_area

27600