-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path939. Minimum Area Rectangle.cpp
43 lines (43 loc) · 1.91 KB
/
939. Minimum Area Rectangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
public:
// STL
int minAreaRect1(vector<vector<int>>& points, int res = INT_MAX) {
unordered_map<int, set<int>> x;
for (auto p : points) x[p[0]].insert(p[1]);
for (auto i = x.begin(); i != x.end(); ++i)
for (auto j = next(i); j != x.end(); ++j) {
if (i->second.size() < 2 || j->second.size() < 2) continue;
vector<int> y;
set_intersection(begin(i->second), end(i->second),
begin(j->second), end(j->second), back_inserter(y));
for (int k = 1; k < y.size(); ++k)
res = min(res, abs(j->first - i->first) * (y[k] - y[k - 1]));
}
return res == INT_MAX ? 0 : res;
}
// another solution
int minAreaRect(vector<vector<int>>& points) {
int MinArea = INT_MAX;
unordered_map<int, unordered_set<int> > mapX;
for(const auto& a: points)
mapX[a[0]].insert(a[1]);
for(int i=0;i<points.size()-1;i++) {
if(mapX[points[i][0]].size()<2)
continue;
for(int j=i+1;j<points.size();j++){
if( points[i][0] == points[j][0] || points[i][1] == points[j][1])
continue;
if((abs(points[i][0] - points[j][0]) * abs( points[i][1] - points[j][1])) >= MinArea)
continue;
if(mapX[ points[j][0] ].size()<2)
continue;
if(mapX[ points[i][0] ].count( points[j][1] ) && mapX[ points[j][0] ].count( points[i][1] ))
MinArea = min( abs(points[i][0] - points[j][0]) * abs( points[i][1] - points[j][1] ),MinArea);
}
}
if(MinArea==INT_MAX) return 0;
return MinArea;
}
};