Find the maximum number of bomb that can be detonated

 Problem

You are given a list of bombs. The range of a bomb is defined as the area where its effect can be felt.

This area is in the shape of a circle with the center as the location of the bomb.

The bombs are represented by a 0-indexed 2D integer array bombs where bombs[i] = [xi, yi, ri].

xi and yi denote the X-coordinate and Y-coordinate of the location of the ith bomb, whereas ri denotes the radius of its range.

You may choose to detonate a single bomb. When a bomb is detonated, it will detonate all bombs that lie in its range. 

These bombs will further detonate the bombs that lie in their ranges.

Given the list of bombs, return the maximum number of bombs that can be detonated if you are allowed to detonate only one bomb.

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

Output: 2

Explanation:

The above figure shows the positions and ranges of the 2 bombs. If we detonate the left bomb, the right bomb will not be affected. But if we detonate the right bomb, both bombs will be detonated.

So the maximum number of bombs that can be detonated is max(1, 2) = 2.


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

Output: 1

Explanation:

Detonating either bomb will not detonate the other bomb, so the maximum number of bombs that can be detonated is 1.

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

Output: 5

Explanation:

The best bomb to detonate is bomb 0 because:

- Bomb 0 detonates bombs 1 and 2. The red circle denotes the range of bomb 0.

- Bomb 2 detonates bomb 3. The blue circle denotes the range of bomb 2.

- Bomb 3 detonates bomb 4. The green circle denotes the range of bomb 3.

Thus all 5 bombs are detonated.


Solution

Initially, I mistook this question for finding the cluster within the graph where two bombs overlap.

Based on that, I wrote the solution using the DFS(Depth-first search) assuming the network of bombs are bidirectional graph. However, my solution did not pass all the test cases.

After reading the question carefully, I realized that "A detonates B" $\neq$ "B detonates A".  This means it is one directional graph and we need to find the base possible path which includes the max number of vertices (bombs).

Based on that idea, I rewrite the code using the DFS and backtracking. The overall running time of this solution is $O(N^2)$ where N is the number of bombs.

UPDATE(03-09-2023): Python solution without the preprocessing. Wrong answer.

This solution can be further optimized if I preprocess each bomb (vertices) to find the reachable next bomb and have them in a list. This will remove the need to run another for loop to check each vertex against all vertices in the cluster. I will add that solution at the bottom. This solution requires a comparison of the next node with all nodes in the solution which could be $O(N)$ (N is # of bombs). This is too slow.

UPDATE(03-09-2023): Python solution with the preprocessing. Correct way.

The original solution had several problems. 

  • Backtracking should not be used. It is still finding the max number of reachable bombs from $B_i$. Therefore we should not backtrack as used in finding the specific path. Algorithm was wrong.
  • It should be viewed as a graph problem and the preprocessing of finding all reachable bombs from $B_i$ should be calculated before we perform the DFS. 
  • Should've used a Set instead of a list to check if any current node had already visited. This will cut the time.

This is the corrected solution. Runs way faster and use less memory.

Comments

Popular posts from this blog

Planting flowers with no adjacent flower plots