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

#define dm 0

#define trace1(x)           if(dm) cout<<(#x)<<" "<<(x)<<endl
#define trace2(x,y)         if(dm) cout<<(#x)<<" "<<(x)<<", "<<(#y)<<" "<<(y)<<endl
#define trace3(x,y,z)       if(dm) cout<<(#x)<<" "<<(x)<<", "<<(#y)<<" "<<(y)<<", "<<(#z)<<" "<<(z)<<endl
#define trace4(x,y,z,w)     if(dm) cout<<(#x)<<" "<<(x)<<", "<<(#y)<<" "<<(y)<<", "<<(#z)<<" "<<(z) << ", " <<(#w)<<" "<<(w)<<endl


class Solution {
public:
    int minAreaRect(vector<vector<int>>& points) {
        const int maxVal = 4*10000;
        vector<vector<int>> x_grp(maxVal+1);
        /*
        0 - 0,1,2,3
        1 - 4
        2 - 1
        3 - 0
        4 - 1,2,4

        */

        for(const vector<int>& pt : points){
            x_grp[pt[0]].push_back(pt[1]);
        }

        vector<pair<int, vector<int> > > pillar;
        for(int x=0; x<=maxVal; x++) if (x_grp[x].size()>=2){
            sort(x_grp[x].begin(), x_grp[x].end());
            pillar.push_back({x, x_grp[x]});
            trace1(x);
        }

        int ans = INT_MAX;

        for(int i=0; i<pillar.size(); i++) 
        for(int j=i+1; j<pillar.size(); j++){
            int x1 = pillar[i].first, x2 = pillar[j].first;
            if(x2 - x1 >= ans) continue;

            vector<int>& y1 = pillar[i].second;
            vector<int>& y2 = pillar[j].second;

            int last_y = -1;
            int idx1=0, idx2=0;
            while(idx1<y1.size() && idx2<y2.size()){
                trace4(idx1, idx2, y1[idx1], y2[idx2]);
                if(y1[idx1]==y2[idx2]){
                    if(last_y != -1){
                        assert(y1[idx1] > last_y);
                        int cur_ans = (y1[idx1] - last_y)*(x2 - x1);
                        if(ans > cur_ans){
                            ans = cur_ans;
                        }
                    }
                    last_y = y1[idx1];

                    idx1++;
                    idx2++;
                }
                else if(y1[idx1]>y2[idx2]){
                    idx2++;
                }
                else{
                    idx1++;
                }
            }

        }

        if(ans==INT_MAX) ans = 0;
        return ans;

    }
};
162234