JJobsMoi
Chime
CH

Detonate the Maximum Bombs

Codingmedium

Problem

You are given a list of circular bombs placed on a 2-D plane. Each bomb is described by [x, y, r], meaning it is located at coordinates (x, y) and has a blast radius r.

If a bomb detonates, every other bomb whose center lies within (or exactly on the edge of) its blast circle will also detonate, and this chain reaction continues: any bomb triggered this way will in turn detonate every bomb inside its own blast circle, and so on. Note that the relationship is not symmetric — bomb A's circle might reach bomb B without B's circle reaching back to A, since the radii can differ.

You may choose exactly one bomb to detonate first. Determine the largest possible number of bombs (including the one you chose to detonate) that can end up exploding.

Examples

Example 1:

Input: bombs = [[2,1,3],[6,1,4]]

Output: 2

Explanation:

The two bombs are 4 units apart. The first bomb has radius 3, so detonating it does not reach the second. The second bomb has radius 4, so detonating it does reach the first (4 <= 4) and triggers both. Choosing the second bomb therefore explodes 2 bombs.

Example 2:

Input: bombs = [[1,1,5],[10,10,5],[10,10,5]]

Output: 2

Explanation:

The first bomb is too far from the other two (distance ~12.7 > 5), so detonating it only sets off itself. Detonating either of the other two bombs, whose centers coincide, reaches the other one (distance 0 <= 5), yielding 2 total.

Example 3:

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

Output: 5

Explanation:

Starting the chain at the first bomb reaches nearby bombs whose radii keep extending the chain reaction until all five bombs have detonated.

Constraints

  • 1 <= bombs.length <= 100
  • bombs[i].length == 3
  • 1 <= bombs[i][j] <= 1000

Solution

Loading editor…

Sign in to get AI feedback on your answer. Your work is saved while you do.

Sign in to evaluate