---
tags: leetcode
---
# [939. Minimum Area Rectangle](https://leetcode.com/problems/minimum-area-rectangle/)
* [詳解](https://hackmd.io/@hsuedw/leetcode939)
---
# My Solution
## The Key Idea for Solving This Coding Question
## C++ Code
```cpp=
class Solution {
public:
int minAreaRect(vector<vector<int>> &points) {
unordered_set<int> hashedPoints;
for (auto &point : points) {
hashedPoints.insert(encode(point[0], point[1]));
}
int minArea = INT_MAX;
for (int i = 0; i < points.size(); ++i) {
int x1 = points[i][0], y1 = points[i][1];
for (int j = 0; j < points.size(); ++j) {
int x3 = points[j][0], y3 = points[j][1];
if (x1 == x3 || y1 == y3) {
continue;
}
if (hashedPoints.count(encode(x1, y3)) == 0 ||
hashedPoints.count(encode(x3, y1)) == 0) {
continue;
}
int len1 = abs(x1 - x3), len2 = abs(y1 - y3);
minArea = min(minArea, len1 * len2);
}
}
if (minArea == INT_MAX) {
return 0;
}
return minArea;
}
private:
int encode(int x, int y) {
return x * 40001 + y;
}
void decode(int code, int &x, int &y) {
x = code / 40001;
y = code % 40001;
}
};
```
## Time Complexity
## Space Complexity
# Miscellane
<!--
# Test Cases
```
[[1,1],[1,3],[3,1],[3,3],[2,2]]
```
```
[[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
```
```
[[3,2],[3,1],[4,4],[1,1],[4,3],[0,3],[0,2],[4,0]]
```
```
[[39973,30270],[13301,37647],[13854,37647],[13301,1045],[13854,1045],[11401,37647],[13854,10339],[11401,1045],[11401,10339],[13301,30270],[39973,37647],[11401,30270],[39973,1045],[39973,10339]]
```
-->