<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:
        
        # 1. put all y in a set, sorted, with x as key
        # 2. for each pair of x, find the intersection of ys
        # 3. for each pair of y after intersection, find the min
        # O(k*k*t) where k is the average # of x and t is the average # of y within a intersection
        
        from sortedcontainers import SortedSet
        
        data = collections.defaultdict(SortedSet)
        for x, y in points:
            data[x].add(y)
        
        all_x = list(data.keys())
        all_x.sort()
        
        l_x = len(all_x)
        
        result = float("inf")
        for i in range(1, l_x):
            s = data[all_x[i]]
            # print(all_x[i], s)
            if len(s) < 2:
                continue
            for k in reversed(range(i)):
                prev_s = data[all_x[k]]
                if len(prev_s) < 2:
                    continue
                # print(all_x[k], prev_s)

                common = s.intersection(prev_s)
                l_c = len(common)
                if l_c < 2:
                    continue
                for j in range(1, l_c):
                    # print(all_x[i], all_x[k], common[j], common[j-1])
                    result = min(result, abs(all_x[i] - all_x[k]) * abs(common[j] - common[j-1]))
                
                # we should not break because the min area may not be from two consective x
        if result == float("inf"):
            return 0
        return result
        # 1. put all y in a set with x as key
        # 2. for a given x, for each pair of y, scan smaller x to say if they also have such y pair
        # O(k*k*t*t) where k is the average number of x and t is the average # of y
        # since k*t == n, so O(n*n)
        
        
        
18100