MediumNo. 1828

Queries on Number of Points Inside a Circle — LeetCode 1828 Python Solution

Topics
Geometry, Array, Math
Reading time
2 min

The problem

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.

Example

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.

Python solution

PythonSolution
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

Complexity

Time
O(m × n)where m and n are the lengths of the arrays `queries` and `points` respectively
Space · auxiliary
O(1)

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1828. Queries on Number of Points Inside a Circle is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math and Geometry.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Related problems
No.ProblemDifficulty
0335Self CrossingHard
0587Erect the FenceHard
0812Largest Triangle AreaEasy
0883Projection Area of 3D ShapesEasy
0892Surface Area of 3D ShapesEasy
1037Valid BoomerangEasy

Frequently asked questions

How hard is LeetCode 1828. Queries on Number of Points Inside a Circle?

LeetCode 1828. Queries on Number of Points Inside a Circle is rated Medium on LeetCode.

What is the time complexity of LeetCode 1828. Queries on Number of Points Inside a Circle?

The Python solution on this page runs in O(m × n), where m and n are the lengths of the arrays `queries` and `points` respectively.

What is the space complexity of LeetCode 1828. Queries on Number of Points Inside a Circle?

The Python solution on this page uses O(1) auxiliary space.

What topics does LeetCode 1828. Queries on Number of Points Inside a Circle cover?

LeetCode 1828. Queries on Number of Points Inside a Circle is tagged Geometry, Array and Math on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.