---
# System prepended metadata

title: 0939. Minimum Area Rectangle
tags: [Leetcode, FaceBook, Medium]

---

# 0939. Minimum Area Rectangle
###### tags: `Leetcode` `Medium` `FaceBook`
Link: https://leetcode.com/problems/minimum-area-rectangle/
## 思路
**把二维转换成一维的思想很重要！**
set里面不能用int[]存，但可以用pair
先把每个二维点转换成一维坐标，存进hashset里面
然后枚举对角线，看另外两个点在不在hashset里面，如果在的话，就算面积
## Code
```java=
class Solution {
    public int minAreaRect(int[][] points) {
        Set<Integer> point = new HashSet<>();
        int c = 0;
        for(int i = 0;i < points.length;i++){
            c = Math.max(c, points[i][1]);
            c += 1;
        }
        for(int i = 0;i < points.length;i++){
            point.add(points[i][0]*c+points[i][1]);
        }
        int minArea = Integer.MAX_VALUE;
        for(int i = 0;i < points.length-1;i++){
            for(int j = i+1;j < points.length;j++){
                int x1 = points[i][0];
                int x2 = points[j][0];
                int y1 = points[i][1];
                int y2 = points[j][1];
                if(x1!=x2 && y1!=y2){
                    if(point.contains(x1*c+y2) && point.contains(x2*c+y1)){
                        minArea = Math.min(minArea, Math.abs((x2-x1)*(y2-y1)));
                    }
                }
            }
        }
        return minArea==Integer.MAX_VALUE?0:minArea;
    }
}
```