939. Minimum Area Rectangle
https://leetcode.com/problems/minimum-area-rectangle/
/*
* @lc app=leetcode id=939 lang=cpp
*
* [939] Minimum Area Rectangle
*/
// @lc code=start
class Solution {
public:
int minAreaRect(vector<vector<int>>& points) {
unordered_map<int, unordered_set<int>> m;
int res = INT_MAX;
for (const auto &p : points) {
m[p[0]].insert(p[1]);
}
for (int i = 0; i < points.size(); ++i) {
for (int j = 0; j < i; ++j) {
if (points[i][0] == points[j][0] || points[i][1] == points[j][1]) continue;
const int x1 = points[i][0], y1 = points[i][1], x2 = points[j][0], y2 = points[j][1];
if (!m[x1].count(y2) || !m[x2].count(y1)) continue;
res = min(res, abs(x1 - x2) * abs(y1 - y2));
}
}
return res == INT_MAX ? 0 : res;
}
};
// @lc code=end
Last updated