963. Minimum Area Rectangle II
题解
矩形的判断条件:1. 对角线的中点重合。2. 对角线长度相同。
对于一组点i,点j,计算获取到对应的中点和对角线长度。
然后按照所有的中点、对角线长度排序,相同的会放在一起。
我们一段段去计算,获取最小的面积值。
由于点是不重复的,所以不会出现对角线中点重合、对角线长度相同,并且面积为0的情况。
最后特判下是否一定有解。
代码
class Solution {
public:
typedef long long LL;
LL get_dist(vector<int> p1, vector<int> p2) {
LL x1 = p1[0], y1 = p1[1];
LL x2 = p2[0], y2 = p2[1];
return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
}
int get_midium(int x1, int x2) {
return (x1 + x2) / 2;
}
double minAreaFreeRect(vector<vector<int>>& points) {
for (auto &p : points) {
p[0] *= 2;
p[1] *= 2;
}
vector<vector<LL>> info;
int n = points.size();
for (int i = 0; i < n; i ++) {
for (int j = i + 1; j < n; j ++) {
int x1 = points[i][0], y1 = points[i][1];
int x2 = points[j][0], y2 = points[j][1];
info.push_back({get_midium(x1, x2), get_midium(y1, y2), get_dist(points[i], points[j]), i, j});
}
}
sort(info.begin(), info.end());
double ans = 1e20;
for (int i = 0; i < info.size(); i ++) {
int j = i + 1;
while(j < info.size() && info[i][0] == info[j][0] && info[i][1] == info[j][1] && info[i][2] == info[j][2]) {
j ++;
}
for (int p = i; p < j; p ++) {
for (int q = p + 1; q < j; q ++) {
int pi = info[p][3], pj = info[p][4];
int qi = info[q][3], qj = info[q][4];
ans = min(ans, sqrt(get_dist(points[pi], points[qj])) * sqrt(get_dist(points[pi], points[qi])));
}
}
}
if (ans == 1e20) {
return 0;
}
return ans / 4;
}
};