[leetcode]最小面积矩形

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014286840/article/details/84350376

939. 最小面积矩形

给定在 xy 平面上的一组点,确定由这些点组成的矩形的最小面积,其中矩形的边平行于 x 轴和 y 轴。

如果没有任何矩形,就返回 0。

示例 1:

输入:[[1,1],[1,3],[3,1],[3,3],[2,2]]
输出:4

示例 2:

输入:[[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
输出:2

提示:

  1. 1 <= points.length <= 500
  2. 0 <= points[i][0] <= 40000
  3. 0 <= points[i][1] <= 40000
  4. 所有的点都是不同的。

C++解法:

class Solution {
public:
    int minAreaRect(vector<vector<int>>& points) {
            set<pair<int, int>> point;
            for (vector<int> v : points)
            {
                point.insert({v[0],v[1]});
            }
            int minValue = INT_MAX;
            for (auto p : point)
            {
                for (auto q : point)
                {
                    if (p.first == q.first || p.second == q.second)
                    {
                        continue;
                    }
                    if (point.count({p.first,q.second}) && point.count({q.first,p.second}))
                    {
                        minValue = min(minValue,
                                       abs((q.first - p.first)*(q.second - p.second)));
                    }
                }
            }
            if (minValue == INT_MAX) {
                return 0;
            }
            return minValue;
    }
};

猜你喜欢

转载自blog.csdn.net/u014286840/article/details/84350376