939. Minimum Area Rectangle

939. Minimum Area Rectangle

Description

You are given an array of points in the X-Y plane points where points[i] = [x<sub>i</sub>, y<sub>i</sub>].

Return the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes. If there is not any such rectangle, return 0.

Example 1:

1
2
Input: points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4

Example 2:

1
2
Input: points = [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2

Constraints:

  • 1 <= points.length <= 500
  • points[i].length == 2
  • 0 <= x<sub>i</sub>, y<sub>i</sub> <= 4 * 10^4
  • All the given points are unique .

Hints/Notes

Solution

Language: C++

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
44
45
46
class Solution {
public:
int minAreaRect(vector<vector<int>>& points) {
map<int, set<int>> Xcoordinates;
map<int, set<int>> Ycoordinates;
for (auto& p : points) {
int x = p[0], y = p[1];
Xcoordinates[x].insert(y);
Ycoordinates[y].insert(x);
}
int res = INT_MAX;
for (auto& [x, s] : Xcoordinates) {
if (s.size() < 2) {
continue;
}
vector<int> yIndex(s.begin(), s.end());
int n = yIndex.size();
for (int i = 0; i < n; i++) {
int y1 = yIndex[i];
if (Ycoordinates[y1].size() < 2 || *Ycoordinates[y1].rbegin() == x) {
continue;
}
for (int j = i + 1; j < n; j++) {
int y2 = yIndex[j];
if (Ycoordinates[y2].size() < 2 || *Ycoordinates[y2].rbegin() == x) {
continue;
}
auto it1 = Ycoordinates[y1].lower_bound(x + 1);
auto it2 = Ycoordinates[y2].lower_bound(x + 1);
while (it1 != Ycoordinates[y1].end() && it2 != Ycoordinates[y2].end()) {
if (*it1 == *it2) {
res = min(res, (*it1 - x) * (y2 - y1));
it1++;
it2++;
} else if (*it1 > *it2) {
it2++;
} else {
it1++;
}
}
}
}
}
return res == INT_MAX ? 0 : res;
}
};