-
Notifications
You must be signed in to change notification settings - Fork 106
/
detect-squares.cpp
66 lines (55 loc) · 1.67 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
// Time: ctor: O(1)
// add: O(1)
// count: O(n)
// Space: O(n)
class DetectSquares {
public:
DetectSquares() {
}
void add(vector<int> point) {
x_to_ys[point[0]].emplace(point[1]);
++point_counts_[point[0]][point[1]];
}
int count(vector<int> point) {
int result = 0;
for (const auto& y : x_to_ys[point[0]]) {
if (y == point[1]) {
continue;
}
const int dy = point[1] - y;
result += point_counts_[point[0]][y] * point_counts_[point[0] + dy][point[1]] * point_counts_[point[0] + dy][y];
result += point_counts_[point[0]][y] * point_counts_[point[0] - dy][point[1]] * point_counts_[point[0] - dy][y];
}
return result;
}
private:
unordered_map<int, unordered_set<int>> x_to_ys;
unordered_map<int, unordered_map<int, int>> point_counts_;
};
// Time: ctor: O(1)
// add: O(1)
// count: O(n)
// Space: O(n)
class DetectSquares2 {
public:
DetectSquares2() {
}
void add(vector<int> point) {
points_.emplace_back(point);
++point_counts_[point[0]][point[1]];
}
int count(vector<int> point) {
int result = 0;
for (const auto& p : points_) {
int x = p[0], y = p[1];
if (!(point[0] != x && point[1] != y && abs(point[0] - x) == abs(point[1] - y))) {
continue;
}
result += point_counts_[point[0]][y] * point_counts_[x][point[1]];
}
return result;
}
private:
vector<vector<int>> points_;
unordered_map<int, unordered_map<int, int>> point_counts_;
};