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>;
3 unordered_map<int, unordered_set<int>> ys;
4 static int cnt[1001][1001];
6 public:
7 DetectSquares() { memset(cnt, 0x00, sizeof(cnt)); }
9 void add(const vector<int> &point) {
10 const int x = point[0];
11 const int y = point[1];
13 cnt[x][y]++;
14 ys[x].insert(y);
15 }
17 int count(const vector<int> &point) {
18 const int x = point[0];
19 const int y = point[1];
20 int res = 0;
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 }
29 return res;
30 }
31 };
33 int DetectSquares::cnt[1001][1001];