[LeetCode] Queries on Number of Points Inside a Circle

1828. Queries on Number of Points Inside a Circle

You are given an array points where points[i] = [xi, yi] is the coordinates of the ith point on a 2D plane. Multiple points can have the same coordinates.

You are also given an array queries where queries[j] = [xj, yj, rj] describes a circle centered at (xj, yj) with a radius of rj.

For each query queries[j], compute the number of points inside the jth circle. Points on the border of the circle are considered inside.

Return an array answer, where answer[j] is the answer to the jth query.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
vector<int> countPoints(vector<vector<int>>& points, vector<vector<int>>& queries) {
vector<int> res;
for(auto& q : queries) {
int cx = q[0], cy = q[1], cr = q[2];
int now = 0;
for(auto& p : points) {
int x = p[0], y = p[1];
int distance = (cx - x) * (cx - x) + (cy - y) * (cy - y);
if(distance <= cr * cr) now++;
}
res.push_back(now);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/08/PS/LeetCode/queries-on-number-of-points-inside-a-circle/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.