leetcode 939. Minimum Area Rectangle

Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.

If there isn’t any rectangle, return 0.

Example 1:

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

Example 2:

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

Note:

  1. 1 <= points.length <= 500
  2. 0 <= points[i][0] <= 40000
  3. 0 <= points[i][1] <= 40000
  4. All points are distinct.

题目给定一系列的点,计算由这些点能构成的最小矩形的面积。
因为只有最多只有500个点,因此是可以暴力过的。
首先选定某一列上的两个点,查找与这两个点相同行的位置上,是否存在两个共列的点,如果有,那么可以构成一个矩形,再计算面积,更新答案


inline bool mmp(vector& a,vector& b){
        if(a[0]==b[0])
            return a[1] column;
    map > > hor,ver;
    map,bool> vis;
public:
    int minAreaRect(vector>& points) {
        int len=points.size();
        if(len<4)
            return 0;
        sort(points.begin(),points.end(),mmp);
        for(int i=0;i star=make_pair(k,ver[k][i].second);
                pair finish=make_pair(-1,-1);
                for(int j=i+1;j& a){
        cout<<"first="<

你可能感兴趣的:(leetcode)