leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0939.cpp (741B)


      1 class Solution {
      2     static int hash(int x, int y) { return 40000 * x + y; }
      3 
      4   public:
      5     int minAreaRect(const vector<vector<int>> &points) const {
      6         const int n = size(points);
      7         unordered_set<int> us;
      8         int res = INT_MAX;
      9 
     10         for (int i = 0; i < n; i++) {
     11             for (int j = 0; j < i; j++) {
     12                 const int x1 = points[i][0], y1 = points[i][1];
     13                 const int x2 = points[j][0], y2 = points[j][1];
     14                 if (us.count(hash(x1, y2)) && us.count(hash(x2, y1))) {
     15                     res = min(res, abs(x1 - x2) * abs(y2 - y1));
     16                 }
     17             }
     18             us.insert(hash(points[i][0], points[i][1]));
     19         }
     20 
     21         return res != INT_MAX ? res : 0;
     22     }
     23 };