classSolution { public: intminAreaRect(vector<vector<int>>& points){ auto cmp = [](constvector<int> &a, constvector<int> &b){ return a[0] < b[0] || a[0] == b[0] && a[1] < b[1]; }; sort(begin(points), end(points), cmp); vector<int> xs; unordered_map<int, vector<int>> m; for (constauto &p : points) { m[p[0]].push_back(p[1]); if (xs.empty() || xs.back() != p[0]) { xs.push_back(p[0]); } } int res = INT_MAX; int nx = xs.size(); for (int i = 0; i < nx; ++i) { if (m[xs[i]].size() == 1) continue; for (int j = i + 1; j < nx; ++j) { if (m[xs[j]].size() == 1) continue; res = min(res, helper(m, xs[i], xs[j])); } } return res == INT_MAX ? 0 : res; }
inthelper(unordered_map<int, vector<int>> &m, int a, int b){ unordered_set<int> s(begin(m[a]), end(m[a])); int n = m[b].size(); int diff = INT_MAX; for (int i = 0; i < n; ++i) { if (!s.count(m[b][i])) continue; int j = i + 1; while (j < n && !s.count(m[b][j])) ++j; if (j == n) break; diff = min(diff, m[b][j] - m[b][i]); i = j - 1; } return diff == INT_MAX ? diff : (b - a) * diff; } };