nikunjsoni

939

Jun 22nd, 2021 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int minAreaRect(vector<vector<int>>& points) {
  4.         int res = INT_MAX;
  5.         unordered_map<int, bool> mp;
  6.         for(const auto& point : points)
  7.             mp[getHash(point[0], point[1])] = true;
  8.        
  9.         for(int i=0; i<points.size()-1; i++){
  10.             for(int j=i+1; j<points.size(); j++){
  11.                 if(points[i][0] == points[j][0] || points[i][1] == points[j][1])
  12.                     continue;
  13.                 if(mp[getHash(points[i][0], points[j][1])] && mp[getHash(points[j][0], points[i][1])])
  14.                     res = min(res, abs(points[i][0] - points[j][0]) * abs(points[i][1] - points[j][1]));
  15.             }
  16.         }
  17.         if(res == INT_MAX)
  18.             return 0;
  19.         return res;
  20.     }
  21.     int getHash(int x, int y){
  22.         return x*40001+y;
  23.     }
  24. };
  25.  
  26. -----------------------------
  27.  
  28. class Solution {
  29. public:
  30.     int minAreaRect(vector<vector<int>>& points) {
  31.         unordered_map<int, unordered_set<int>> mp;
  32.         int res = INT_MAX;
  33.        
  34.         for(const auto& point : points)
  35.             mp[point[0]].insert(point[1]);
  36.        
  37.         for(int i=0; i<points.size()-1; i++){
  38.             for(int j=i+1; j<points.size(); j++){
  39.                 if(points[i][0] == points[j][0] || points[i][1] == points[j][1])
  40.                     continue;
  41.                 if(mp[points[i][0]].count(points[j][1]) && mp[points[j][0]].count(points[i][1]))
  42.                     res = min(res, abs(points[i][0] - points[j][0]) * abs(points[i][1] - points[j][1]));
  43.             }
  44.         }
  45.         if(res == INT_MAX)
  46.             return 0;
  47.         return res;
  48.     }
  49. };
Add Comment
Please, Sign In to add comment