Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/1828.html
1828. Queries on Number of Points Inside a Circle
Level
Medium
Description
You are given an array points
where points[i] = [x_i, y_i]
is the coordinates of the i-th
point on a 2D plane. Multiple points can have the same coordinates.
You are also given an array queries
where queries[j] = [x_j, y_j, r_j]
describes a circle centered at (x_j, y_j)
with a radius of r_j
.
For each query queries[j]
, compute the number of points inside the j-th
circle. Points on the border of the circle are considered inside.
Return an array answer
, where answer[j]
is the answer to the j-th
query.
Example 1:
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.
Example 2:
Input: points = [[1,1],[2,2],[3,3],[4,4],[5,5]], queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]]
Output: [2,3,2,4]
Explanation: The points and circles are shown above.
queries[0] is green, queries[1] is red, queries[2] is blue, and queries[3] is purple.
Constraints:
1 <= points.length <= 500
points[i].length == 2
0 <= x_i, y_i <= 500
1 <= queries.length <= 500
queries[j].length == 3
0 <= x_j, y_j <= 500
1 <= r_j <= 500
- All coordinates are integers.
Solution
For each query, obtain the values x
, y
and r
, and for each point, calculate the squared distance between the query’s center and the point. If the squared distance is less than or equal to r * r
, then the point is inside the circle. The number of points inside each circle can be calculated.
-
class Solution { public int[] countPoints(int[][] points, int[][] queries) { int queriesCount = queries.length; int[] counts = new int[queriesCount]; for (int i = 0; i < queriesCount; i++) { int[] query = queries[i]; int x = query[0], y = query[1], r = query[2]; int rSquare = r * r; for (int[] point : points) { int distanceSquare = getDistanceSquare(point, x, y); if (distanceSquare <= rSquare) counts[i]++; } } return counts; } public int getDistanceSquare(int[] point, int x, int y) { return (x - point[0]) * (x - point[0]) + (y - point[1]) * (y - point[1]); } } ############ class Solution { public int[] countPoints(int[][] points, int[][] queries) { int m = queries.length; int[] ans = new int[m]; for (int k = 0; k < m; ++k) { int x = queries[k][0], y = queries[k][1], r = queries[k][2]; for (var p : points) { int i = p[0], j = p[1]; int dx = i - x, dy = j - y; if (dx * dx + dy * dy <= r * r) { ++ans[k]; } } } return ans; } }
-
// OJ: https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/ // Time: O(PQ) // Space: O(1) class Solution { public: vector<int> countPoints(vector<vector<int>>& P, vector<vector<int>>& Q) { vector<int> ans; for (auto &q : Q) { int cnt = 0; for (auto &p : P) { if (pow(q[0] - p[0], 2) + pow(q[1] - p[1], 2) <= pow(q[2], 2)) ++cnt; } ans.push_back(cnt); } return ans; } };
-
class Solution: def countPoints( self, points: List[List[int]], queries: List[List[int]] ) -> List[int]: ans = [] for x, y, r in queries: cnt = 0 for i, j in points: dx, dy = i - x, j - y cnt += dx * dx + dy * dy <= r * r ans.append(cnt) return ans ############ # 1828. Queries on Number of Points Inside a Circle # https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/ class Solution: def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]: res = [] for x, y, r in queries: count = 0 for p1,p2 in points: dx = abs(p1 - x) dy = abs(p2 - y) count += int(dx**2 + dy**2 <= r**2) res.append(count) return res
-
func countPoints(points [][]int, queries [][]int) (ans []int) { for _, q := range queries { x, y, r := q[0], q[1], q[2] cnt := 0 for _, p := range points { i, j := p[0], p[1] dx, dy := i-x, j-y if dx*dx+dy*dy <= r*r { cnt++ } } ans = append(ans, cnt) } return }
-
function countPoints(points: number[][], queries: number[][]): number[] { return queries.map(([cx, cy, r]) => { let res = 0; for (const [px, py] of points) { if (Math.sqrt((cx - px) ** 2 + (cy - py) ** 2) <= r) { res++; } } return res; }); }
-
impl Solution { pub fn count_points(points: Vec<Vec<i32>>, queries: Vec<Vec<i32>>) -> Vec<i32> { queries .iter() .map(|v| { let cx = v[0]; let cy = v[1]; let r = v[2].pow(2); let mut count = 0; for p in points.iter() { if ((p[0] - cx).pow(2) + (p[1] - cy).pow(2)) <= r { count += 1; } } count }) .collect() } }