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)


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