intcount(vector<int> point){ // iterate in Y axis, iterate in X axis // time complexity: n constint x = point[0]; constint y = point[1]; constauto& row = points[x]; int ans = 0; for (constauto& p : points) { if (p.first == x) continue; constauto& row2 = p.second; auto it = row2.find(y); if (it != row2.end()) { // find D constint width = abs(p.first - x); for (int i : {+width, -width}) { auto itB = row.find(y + i); auto itC = row2.find(y + i); if (itB != row.end() && itC != row2.end()) { ans += it->second * itB->second * itC -> second; } } } } return ans; } };
/** * Your DetectSquares object will be instantiated and called as such: * DetectSquares* obj = new DetectSquares(); * obj->add(point); * int param_2 = obj->count(point); */