> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Detonate the Maximum Bombs Python Solution

> Tested Python solution for LeetCode 2101 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2101, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [Depth-First Search](/catalog/topics/depth-first-search), [Breadth-First Search](/catalog/topics/breadth-first-search), [Graph Theory](/catalog/topics/graph-theory), [Geometry](/catalog/topics/geometry). [View on LeetCode](https://leetcode.com/problems/detonate-the-maximum-bombs/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2101   # by problem number
lcpy gen -s detonate_the_maximum_bombs   # by problem name
```

## 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] = [x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>]`. `x<sub>i</sub>` and `y<sub>i</sub>` denote the X-coordinate and Y-coordinate of the location of the `i<sup>th</sup>` bomb, whereas `r<sub>i</sub>` 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*.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/11/06/desmos-eg-3.png)

```
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 bombs that can be detonated is max(1, 2) = 2.
```

![Example 2](https://assets.leetcode.com/uploads/2021/11/06/desmos-eg-2.png)

```
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.
```

![Example 3](https://assets.leetcode.com/uploads/2021/11/07/desmos-eg1.png)

```
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.
```

### Constraints

* 1 \<= bombs.length \<= 100
* bombs\[i].length == 3
* 1 \<= x\<sub>i\</sub>, y\<sub>i\</sub>, r\<sub>i\</sub> \<= 10\<sup>5\</sup>

**Follow up:** Can you solve it without building the graph explicitly?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/detonate_the_maximum_bombs/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/detonate_the_maximum_bombs/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^3) in the worst case (n BFS passes over an O(n^2) adjacency build)
    # Space: O(n^2)
    def maximum_detonation(self, bombs: list[list[int]]) -> int:
        n = len(bombs)
        adj: list[list[int]] = [[] for _ in range(n)]
        for i, (xi, yi, ri) in enumerate(bombs):
            for j, (xj, yj, _) in enumerate(bombs):
                if i != j and (xi - xj) ** 2 + (yi - yj) ** 2 <= ri * ri:
                    adj[i].append(j)

        def bfs(start: int) -> int:
            seen = [False] * n
            seen[start] = True
            stack = [start]
            count = 0
            while stack:
                node = stack.pop()
                count += 1
                for nxt in adj[node]:
                    if not seen[nxt]:
                        seen[nxt] = True
                        stack.append(nxt)
            return count

        return max(bfs(i) for i in range(n))
```

## Complexity

| Time | Space |
| - | - |
| O(n^3) in the worst case (n BFS passes over an O(n^2) adjacency build) | O(n^2) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.