rect is parellel with x, y cord

                Never    
C++
       
class Solution {
public:
    double maxAreaRect(vector<vector<double>>& p){
        double n = p.size(), max_area = -1; // store max area
        
        // key -> x(double), val -> y with same x(unordered_set of double)
        unordered_map<double, unordered_set<double>> m; // a hash map
        // use unordered data structure because sorted structure might cost more time when sorting for us
        for(i = 0; i<n; i++) m[p[i][0]].insert(p[i][1]);
        
        for(int i = 0; i < n; i++) { // Iterator corresponding to different A points
            for(int j = i+1; j < n; j++) { // Iterator corresponding to different B points
                // find 2 random different points first
                double x1 = p[i][0], y1 = p[i][1], x2 = p[j][0], y2 = p[j][1]; 
                if(x1 != x2 && y1 != y2){ // if 2 points forms a diagonal
                    // check if the other 2 points of the rectangle exists
                    if(m[x1].count(y2) && m[x2].count(y1)) 
                        max_area = max(max_area, abs((x1-x2)*(y1-y2));
                }
            }
        }
        
        return max_area;
    }
};

Raw Text