forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2013-Detect-Squares.cpp
53 lines (41 loc) · 1.36 KB
/
2013-Detect-Squares.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
Given stream of points, add new points, return count of squares
Find diagonals, if exists then forms a square, loop thru all points
Time: O(1) add O(n^2) count -> n = number of points
Space: O(n)
*/
class DetectSquares {
public:
DetectSquares() {
}
void add(vector<int> point) {
points[point[0]][point[1]]++;
}
int count(vector<int> point) {
int x1 = point[0];
int y1 = point[1];
int result = 0;
for (auto x = points.begin(); x != points.end(); x++) {
unordered_map<int, int> yPoints = x->second;
for (auto y = yPoints.begin(); y != yPoints.end(); y++) {
int x3 = x->first;
int y3 = y->first;
// skip points on same x-axis or y-axis
if (abs(x3 - x1) == 0 || abs(x3 - x1) != abs(y3 - y1)) {
continue;
}
result += points[x3][y3] * points[x1][y3] * points[x3][y1];
}
}
return result;
}
private:
// {x -> {y -> count}}
unordered_map<int, unordered_map<int, int>> points;
};
/**
* Your DetectSquares object will be instantiated and called as such:
* DetectSquares* obj = new DetectSquares();
* obj->add(point);
* int param_2 = obj->count(point);
*/