699. Chain Reaction Blasts

A demolition crew has n bombs on a flat field. Bomb i is given as bombs[i] = [x, y, r]: it sits at (x, y) and its blast reaches every bomb whose position is within distance r of it (a distance of exactly r counts). A bomb that is reached explodes too, using its own radius, which can reach further bombs, and so on. Blasts are not symmetric: a big bomb can set off a small one without the reverse happening.

You may trigger exactly one bomb by hand. Return the largest number of bombs, including the first, that can explode as a result. Coordinates and radii reach 10^5, so squared distances need 64-bit integers. Aim for O(n^3) time at worst.

Example 1

Input:
bombs = [[0,0,5],[4,0,5],[9,0,2],[20,0,1]]
Output:
3
Explanation:

Bomb 0 (radius 5) reaches bomb 1 at distance 4, and bomb 1 (radius 5) touches bomb 2 at distance exactly 5. Bomb 2 is too small to reach back. Bomb 3 is far away. Triggering bomb 0 or 1 sets off 3 bombs.

Example 2

Input:
bombs = [[0,0,1],[10,0,1],[0,10,1]]
Output:
1
Explanation:

The bombs are 10 units apart but have radius 1, so no blast reaches another bomb.

Example 3

Input:
bombs = [[1,1,3],[2,2,1],[5,1,1],[6,1,2]]
Output:
2
Explanation:

Bomb 0 reaches bomb 1 only (distance squared 2), but bomb 1 cannot reach anything else. Bombs 2 and 3 also set each other off, so the best is 2.

Constraints

1 ≤ n ≤ 300

0 ≤ x, y ≤ 100000

0 ≤ r ≤ 100000

Several bombs may share the same position.

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 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms

Expected complexity

Time
O(n^3)
Space
O(n^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…