Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SuryaPratapK/2a37bad876c274dd3d545bd8654ae4fc to your computer and use it in GitHub Desktop.
Save SuryaPratapK/2a37bad876c274dd3d545bd8654ae4fc to your computer and use it in GitHub Desktop.
class Solution {
#define ll long long
public:
long long largestSquareArea(vector<vector<int>>& bottomLeft, vector<vector<int>>& topRight) {
int n = bottomLeft.size();
vector<vector<int>> rectangles(n,vector<int>(4));
for(int i=0;i<n;i++){
rectangles[i][0] = bottomLeft[i][0];
rectangles[i][1] = bottomLeft[i][1];
rectangles[i][2] = topRight[i][0];
rectangles[i][3] = topRight[i][1];
}
sort(rectangles.begin(),rectangles.end());
ll maxArea = 0;
//Find intersection square area with all possible pairs
int xi1,xi2,yi1,yi2; //i-th Rectangle
int xj1,xj2,yj1,yj2; //j-th Rectangle
int blx,bly;//bottom-left x,y
int trx,try_;//top-right x,y
ll sq_side;//side of max-square enclosed
for(int i=0;i<n-1;++i){
for(int j=i+1;j<n;++j){
//Find all lines
xi1 = rectangles[i][0];
xi2 = rectangles[i][2];
yi1 = rectangles[i][1];
yi2 = rectangles[i][3];
xj1 = rectangles[j][0];
xj2 = rectangles[j][2];
yj1 = rectangles[j][1];
yj2 = rectangles[j][3];
//Find if the current pair intersect
if(yj1>=yi1 and yj1<=yi2){ //Case-A
if(xj1>=xi1 and xj1<=xi2){
blx = xj1;
bly = yj1;
trx = min(xj2,xi2);
try_ = min(yi2,yj2);
sq_side = min(trx-blx,try_-bly);
maxArea = max(maxArea,sq_side*sq_side);
}
} else if(yj2>=yi1 and yj2<=yi2){ //Case-B
if(xj1>=xi1 and xj1<=xi2){
blx = xj1;
bly = yi1;
trx = min(xi2,xj2);
try_ = yj2;
sq_side = min(trx-blx,try_-bly);
maxArea = max(maxArea,sq_side*sq_side);
}
} else if(xj1>=xi1 and xj1<=xi2){ //Case-C
if(yj1<yi1 and yj2>yi2){
blx = xj1;
bly = yi1;
trx = min(xj2,xi2);
try_ = yi2;
sq_side = min(trx-blx,try_-bly);
maxArea = max(maxArea,sq_side*sq_side);
}
}
}
}
return maxArea;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment