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 arrayanswer, whereanswer[j]is the answer to thejthquery.

Input: points =[[1,3],[3,3],[5,3],[2,2]], queries =[[2,3,1],[4,3,1],[1,1,2]]Output: [3,2,2]Explanation: The points and circles are shown above.queries[0]is the green circle, queries[1]is the red circle, and queries[2]is the blue circle.
For each query, check every point to see if it lies inside or on the border of the circle using the distance formula. This is efficient enough for the given constraints.
classSolution {
publicint[]countPoints(int[][] points, int[][] queries) {
int[] ans =newint[queries.length];
for (int i = 0; i < queries.length; ++i) {
int x = queries[i][0], y = queries[i][1], r2 = queries[i][2]* queries[i][2];
int cnt = 0;
for (int[] p : points) {
int dx = p[0]- x, dy = p[1]- y;
if (dx*dx + dy*dy <= r2) ++cnt;
}
ans[i]= cnt;
}
return ans;
}
}
from typing import List
classSolution:
defcountPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:
ans = []
for x, y, r in queries:
cnt =0for xi, yi in points:
if (xi - x) **2+ (yi - y) **2<= r * r:
cnt +=1 ans.append(cnt)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
impl Solution {
pubfncount_points(points: Vec<Vec<i32>>, queries: Vec<Vec<i32>>) -> Vec<i32> {
queries.iter().map(|q| {
let (x, y, r2) = (q[0], q[1], q[2]*q[2]);
points.iter().filter(|p| {
let dx = p[0] - x;
let dy = p[1] - y;
dx*dx + dy*dy <= r2
}).count() asi32 }).collect()
}
}