leetcode

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

2013.cpp (860B)


      1 class DetectSquares {
      2     using point_t = tuple<int, int>;
      3 
      4     unordered_map<int, unordered_set<int>> ys;
      5     static int cnt[1001][1001];
      6 
      7   public:
      8     DetectSquares() { memset(cnt, 0x00, sizeof(cnt)); }
      9 
     10     void add(const vector<int> &point) {
     11         const int x = point[0];
     12         const int y = point[1];
     13 
     14         cnt[x][y]++;
     15         ys[x].insert(y);
     16     }
     17 
     18     int count(const vector<int> &point) {
     19         const int x = point[0];
     20         const int y = point[1];
     21         int res = 0;
     22 
     23         for (const auto yp : ys[point[0]]) {
     24             const int len = abs(y - yp);
     25             if (!len) continue;
     26             if (x + len <= 1000) res += cnt[x][yp] * cnt[x + len][y] * cnt[x + len][yp];
     27             if (x >= len) res += cnt[x][yp] * cnt[x - len][y] * cnt[x - len][yp];
     28         }
     29 
     30         return res;
     31     }
     32 };
     33 
     34 int DetectSquares::cnt[1001][1001];