-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC1828.cpp
More file actions
executable file
·37 lines (32 loc) · 986 Bytes
/
LC1828.cpp
File metadata and controls
executable file
·37 lines (32 loc) · 986 Bytes
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
/*
Problem Statement: https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/
Time: O(n • log n + n • q)
Space: O(n • q)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
private:
int euclidean_distance(int x1, int y1, int x2, int y2) {
int x = x2 - x1, y = y2 - y1;
return x * x + y * y;
}
public:
vector<int> countPoints(vector<vector<int>> points, vector<vector<int>>& queries) {
int n = points.size(), q = queries.size();
vector<int> res(q);
sort(points.begin(), points.end());
for (int i = 0; i < q; i++) {
int rr, pos;
vector<int>& q = queries[i];
rr = q[2] * q[2];
pos = distance(points.begin(), lower_bound(points.begin(), points.end(), q[0] - q[2], [](auto& p, int x) {
return p[0] < x;
}));
for (int j = pos; j < n && points[j][0] <= q[0] + q[2]; j++) {
vector<int>& p = points[j];
res[i] += (euclidean_distance(p[0], p[1], q[0], q[1]) <= rr);
}
}
return res;
}
};