1001. Points in Circles

Medium3.5 points estimatedGeometryMathsArray3 hints

A rescue map is a square grid with integer coordinates from 0 to 400, and points[i] = [x, y] is a sensor (several sensors may share a spot). Each entry [cx, cy, r] of circles describes a radio beacon centred at (cx, cy) with radius r; the centre may lie anywhere, even outside the map.

For every beacon return how many sensors are within distance r of its centre, a sensor exactly at distance r counting as inside. Answer in the same order as circles.

Looking at every sensor for every beacon is too slow. Aim for about O(n + C^2 + q*C) time and O(C^2) space, where C = 401 is the grid side.

Example 1

Input:
points = [[3,4],[1,1],[5,5],[2,6],[8,2],[5,5]]circles = [[3,3,2],[6,4,3],[0,0,1],[9,9,0],[5,5,0]]
Output:
[1,4,0,0,2]
Explanation:

Circle (3,3,2) holds only the sensor at (3,4); circle (6,4,3) holds (3,4), (8,2) and both sensors at (5,5); the radius-0 circle at (5,5) still holds the two sensors that share that spot.

Example 2

Input:
points = [[10,20],[30,20],[20,40],[20,5]]circles = [[20,20,10],[20,20,20],[25,30,3]]
Output:
[2,4,0]
Explanation:

Points exactly on the border count, so circle (20,20,10) holds (10,20) and (30,20); radius 20 additionally reaches (20,40) and (20,5); the small circle at (25,30) holds nothing.

Constraints

1 ≤ n = points.length ≤ 105; 0 ≤ x, y ≤ 400

0 ≤ q = circles.length ≤ 2000

0 ≤ cx, cy, r ≤ 104

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,600 msC++ 400 msJava 800 msJavaScript 800 msTypeScript 800 ms

Expected complexity

Time
O(n + C^2 + q*C)
Space
O(C^2)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…