Leetcode 1828: Queries on Number of Points Inside a Circle

grid47
grid47
Exploring patterns and algorithms
May 8, 2024 7 min read

You are given an array of points, each represented by its coordinates [xi, yi] on a 2D plane. You are also given an array of queries, where each query describes a circle defined by its center at (xj, yj) and its radius rj. For each query, your task is to compute how many points lie inside or on the border of the circle.
Problem
Approach
Steps
Complexity
Input: You are given two arrays. The first array, points, consists of pairs of integers where each pair represents the coordinates of a point. The second array, queries, consists of triplets where each triplet represents the center coordinates (xj, yj) and the radius rj of a circle.
Example: For example, points = [[1,3], [3,3], [5,3], [2,2]] represents 4 points, and queries = [[2,3,1], [4,3,1], [1,1,2]] represents 3 circles.
Constraints:
• 1 <= points.length <= 500
• points[i].length == 2
• 0 <= xi, yi <= 500
• 1 <= queries.length <= 500
• queries[j].length == 3
• 0 <= xj, yj <= 500
• 1 <= rj <= 500
Output: Return an array of integers where each element is the number of points inside or on the border of the corresponding circle defined in the queries array.
Example: For points = [[1,3],[3,3],[5,3],[2,2]] and queries = [[2,3,1],[4,3,1],[1,1,2]], the output will be [3, 2, 2], as there are 3 points inside or on the first circle, 2 points inside or on the second, and 2 points inside or on the third circle.
Constraints:
• Each answer in the result should be an integer.
Goal: For each query, calculate how many points are inside the circle. The circle includes the points on its border as well.
Steps:
• 1. For each query, loop through each point and calculate the squared distance from the point to the center of the circle.
• 2. If the squared distance is less than or equal to the squared radius, increment the count for that query.
• 3. After processing all points for a query, store the count as the result for that query.
Goal: Make sure to handle the queries efficiently within the given constraints of the input sizes.
Steps:
• Each query's circle must be evaluated for all points in the input array.
• The input size can go up to 500 points and 500 queries.
Assumptions:
• Each point is represented as a pair of integers (xi, yi), and each query as a triplet (xj, yj, rj).
• A point is considered inside or on the border of the circle if its distance from the center is less than or equal to the radius.
Input: Input: points = [[1,3], [3,3], [5,3], [2,2]], queries = [[2,3,1], [4,3,1], [1,1,2]]
Explanation: The first circle is centered at (2,3) with radius 1, which includes the points [1,3], [2,2], and [3,3], so the answer is 3. The second circle at (4,3) with radius 1 includes points [3,3] and [5,3], so the answer is 2. The third circle at (1,1) with radius 2 includes points [1,3] and [2,2], so the answer is 2.

Input: 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]]
Explanation: The first circle includes the points [1,1] and [2,2], so the answer is 2. The second circle includes [1,1], [2,2], and [3,3], so the answer is 3. The third circle includes [4,4] and [5,5], so the answer is 2. The fourth circle includes all points, so the answer is 4.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus