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

/*

   method:
     1. X, hash: O(nlogn)
        sort first,
        keep a hash table, h[y] = x, record the max(x) for the same y
        
        then for each row (i.e x)
           keep another hash h2[x] = y, record the max(y) in the previous row which having the y
           if any h2[x] is found (a previous match point found), a rectangle found
           
        why X? think[[0,1],[0,2][1,1],[2,1],[2,2]]]
             [1,1] will overlap [0,1] so when checking row 2, it can not see [0,1]
           
     2. row X row comparison: O(n^2)
        sort elements in each row
        like merge sort, for 2 rows(said, x1, x2),
        if any 2 same y found(said y1), keep it as prev = y1
        if next same y meet(said, y2), the area = (y2-y1)*(x2-x1) 
        
*/
class Solution {
public:
    int minAreaRect(vector<vector<int>>& points) {
        int n = points.size();
        sort(points.begin(), points.end());
        
        // loop i to the 2nd row
        int i = 0;
        while (i < n && points[i][0] == points[0][0])
            ++i;
        
        int res = INT_MAX;
        while (i < n) {
            int j = 0, r1 = points[i][0], k;
            while (j < i) {
                k = i;
                int prev = -1, r2 = points[j][0];
                int x_diff = points[i][0] - r2;
                while (k < n && points[k][0] == r1 && points[j][0] == r2) {
                    if (points[k][1] == points[j][1]) {
                        if (prev >= 0)
                            res = min(res, x_diff*(points[k][1] - prev));
                        prev = points[k][1];
                        ++k, ++j;
                    } else if (points[k][1] < points[j][1])
                        ++k;
                    else
                        ++j;
                }
                
                // row might be walk over, keep walking till next row
                while (j < i && points[j][0] == r2)
                    ++j;
            }
            
            while (k < n && points[k][0] == r1)
                ++k;
            i = k;
        }
        
        return res == INT_MAX ? 0 : res;
    }
};
23784