1021. Points in Circles

Medium3.5 points estimatedGeometryMathsArray3 hints

A city places n street lamps on an integer grid; lamp i stands at points[i] = [x, y], and several lamps can share one spot. A radio company then tests broadcast towers: tower j is described by circles[j] = [cx, cy, r] and reaches every spot whose straight-line distance from (cx, cy) is at most r, boundary included. A radius of 0 reaches only its own spot.

For each tower, count how many lamps it reaches. Return an array result in which result[j] is the lamp count for circles[j]. Towers do not affect each other; every tower is evaluated against all lamps.

Example 1

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

Circle [2,2,2] holds (2,3) and (1,1); circle [6,5,1] holds only (6,6); circle [0,0,1] holds none of the lamps.

Example 2

Input:
points = [[4,4],[4,4],[7,4]]circles = [[4,4,0],[5,4,3]]
Output:
[2,3]
Explanation:

A radius-0 circle at (4,4) contains the two lamps standing exactly there; the circle centred (5,4) with radius 3 reaches all three lamps (the one at (7,4) is at distance 2).

Example 3

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

The point (3,4) lies exactly on the boundary of radius 5 and counts; with radius 4 only (0,0) remains.

Constraints

  • 1 ≤ points.length ≤ 105
  • 1 ≤ circles.length ≤ 104
  • 0 ≤ x, y, cx, cy ≤ 300
  • 0 ≤ r ≤ 50

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 400 msC++ 100 msJava 200 msJavaScript 200 msTypeScript 200 ms

Expected complexity

Time
O(C^2 + q * r) with C = 301
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…