Skip to content

Instantly share code, notes, and snippets.

@QApolo
Last active September 7, 2020 03:50
Show Gist options
  • Save QApolo/60e962e7dbcd8ec0bcc4b0e3f4f6a4d2 to your computer and use it in GitHub Desktop.
Save QApolo/60e962e7dbcd8ec0bcc4b0e3f4f6a4d2 to your computer and use it in GitHub Desktop.
class Solution {
public:
int minAreaRect(vector<vector<int>>& points) {
if(points.size() < 4)
return 0;
unordered_map <int, unordered_map<int, bool>> side;
for(int i = 0; i < points.size(); i++) {
side[points[i][0]][points[i][1]] = true;
}
int ans = numeric_limits<int>::max();
int x1, x2, x3, x4;
int y1, y2, y3, y4;
for(int i = 0; i < points.size(); i++) {
for(int j = i + 1; j < points.size(); j++) {
int a = abs(points[j][0] - points[i][0]);
int b = abs(points[j][1] - points[i][1]);
if(!(a == 0 or b == 0)) {
x1 = points[i][0];
y1 = points[i][1];
x2 = x1 + a;
y3 = y1 - b;
x4 = x1 + a;
y4 = y1 - b;
if(side[x1][y1] and side[x2][y1] and side[x1][y3] and side[x4][y4]) {
ans = min(ans, a*b);
}
x2 = x1 - a;
y3 = y1 + b;
x4 = x1 - a;
y4 = y1 + b;
if(side[x1][y1] and side[x2][y1] and side[x1][y3] and side[x4][y4]) {
ans = min(ans, a*b);
}
}
}
}
if(ans == numeric_limits<int>::max())
return 0;
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment