LeetCode 1828: Queries on Number of Points Inside a Circle

ArrayMathGeometry

Problem Description

Explanation:

To solve this problem, we can iterate through each query and each point to check if the point lies inside the circle defined by the query. For each query, we calculate the distance between the center of the circle and each point. If this distance is less than or equal to the radius of the circle, we increment the count of points inside the circle.

Algorithm:

  1. For each query, iterate through each point.
  2. Calculate the distance between the center of the circle and the point using the distance formula.
  3. If the distance is less than or equal to the radius of the circle, increment the count of points inside the circle.
  4. Store the count for each query in the answer array.

Time Complexity: O(n * m) where n is the number of points and m is the number of queries.

Space Complexity: O(1) (excluding the space required for the output).

Solutions

class Solution {
    public int[] countPoints(int[][] points, int[][] queries) {
        int[] answer = new int[queries.length];
        
        for (int i = 0; i < queries.length; i++) {
            for (int j = 0; j < points.length; j++) {
                int distance = (int) (Math.pow((points[j][0] - queries[i][0]), 2) + Math.pow((points[j][1] - queries[i][1]), 2));
                if (distance <= Math.pow(queries[i][2], 2)) {
                    answer[i]++;
                }
            }
        }
        
        return answer;
    }
}

Loading editor...