Loading problems...
TL;DR: Model the bombs as a directed graph where edges represent detonation reach, then perform a Depth-First Search (DFS) from every bomb to find the starting point that yields the largest set of reachable nodes.
The Detonate the Maximum Bombs problem asks us to determine the maximum number of bombs that can be triggered by detonating a single chosen bomb. Each bomb has a circular range defined by a radius. When a bomb detonates, it triggers all other bombs whose centers lie within its circular range. This creates a chain reaction. Because radii differ, the relationship is not necessarily mutual; a large bomb might trigger a small one, but the small one might not trigger the large one.
This is a classic application of graph theory on a geometric dataset, making LeetCode 2101 a popular interview question for testing the ability to translate physical constraints into data structures.
A naive interpretation of the problem might involve simulating the chain reaction step-by-step without constructing a formal graph structure.
While this approach logically finds the solution, it performs redundant geometric calculations. For every step in the chain reaction, we might iterate through the entire array to check distances, leading to significant overhead. Specifically, checking reachability "on the fly" requires repeated distance formula calculations () for every node visited in every traversal, resulting in a time complexity closer to or depending on implementation details.
bombsAlthough the constraint allows this to pass, explicitly modeling the relationships as a graph first is cleaner, faster, and demonstrates the correct engineering pattern.
The key insight is to abstract the geometry away immediately. Instead of thinking about circles and coordinates during the traversal, we pre-process the input into a Directed Graph.
Because the graph is directed, the set of reachable nodes depends entirely on the starting node. Therefore, to find the maximum number of detonations, we must run a graph traversal (DFS) starting from each node independently and count the number of visited nodes for that specific run.
Visual Description: Imagine the bombs as nodes in space. Draw an arrow from Bomb A to Bomb B only if B is inside A's circle. We are looking for the "root" node that has paths leading to the highest number of other nodes. The algorithm visualizes this by lighting up a start node, following all outgoing arrows recursively, and counting the total number of lit nodes.

Let's trace the algorithm with a simple example: bombs = [[0,0,2], [0,3,2]].
Build Graph:
0: [], 1: [].Iterate Sources:
visited = {0}.[0]. Pop 0. Neighbors: None.max_detonations = 1.visited = {1}.[1]. Pop 1. Neighbors: None.max_detonations = 1.Return: 1.
Consider a connected example: A -> B.
The correctness relies on the faithful representation of the physical problem as a graph. The condition is the exact mathematical definition of "Bomb is in range of Bomb ." By constructing directed edges based on this inequality, the graph topology perfectly mirrors the detonation logic.
The DFS algorithm is guaranteed to visit every node reachable from a starting vertex in a finite graph. By resetting the visited state and running DFS for every possible start node, we exhaustively check the reachability potential of every bomb. Thus, the maximum value found is guaranteed to be the global maximum.
cppclass Solution { public: int maximumDetonation(vector<vector<int>>& bombs) { int n = bombs.size(); // Adjacency list for the directed graph vector<vector<int>> graph(n); // Build the graph for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; long long x1 = bombs[i][0], y1 = bombs[i][1], r1 = bombs[i][2]; long long x2 = bombs[j][0], y2 = bombs[j][1]; // Calculate squared distance to avoid sqrt precision issues long long distSq = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); long long rSq = r1 * r1; if (distSq <= rSq) { graph[i].push_back(j); } } } int maxBombs = 0; // Try detonating each bomb as the starting point for (int i = 0; i < n; i++) { int count = 0; vector<bool> visited(n, false); dfs(i, visited, count, graph); maxBombs = max(maxBombs, count); } return maxBombs; } private: void dfs(int node, vector<bool>& visited, int& count, const vector<vector<int>>& graph) { visited[node] = true; count++; for (int neighbor : graph[node]) { if (!visited[neighbor]) { dfs(neighbor, visited, count, graph); } } } };
javaimport java.util.*; class Solution { public int maximumDetonation(int[][] bombs) { int n = bombs.length; List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } // Build the directed graph for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; long x1 = bombs[i][0], y1 = bombs[i][1], r1 = bombs[i][2]; long x2 = bombs[j][0], y2 = bombs[j][1]; // Use long to prevent overflow during squaring long distSq = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); long rSq = r1 * r1; if (distSq <= rSq) { graph.get(i).add(j); } } } int maxBombs = 0; // DFS from each node to find maximum reachability for (int i = 0; i < n; i++) { maxBombs = Math.max(maxBombs, dfs(i, new boolean[n], graph)); } return maxBombs; } private int dfs(int node, boolean[] visited, List<List<Integer>> graph) { visited[node] = true; int count = 1; for (int neighbor : graph.get(node)) { if (!visited[neighbor]) { count += dfs(neighbor, visited, graph); } } return count; } }
pythonclass Solution: def maximumDetonation(self, bombs: List[List[int]]) -> int: n = len(bombs) graph = [[] for _ in range(n)] # Build the directed graph for i in range(n): for j in range(n): if i == j: continue x1, y1, r1 = bombs[i] x2, y2, _ = bombs[j] # Check if bomb j is within range of bomb i dist_sq = (x1 - x2)**2 + (y1 - y2)**2 if dist_sq <= r1**2: graph[i].append(j) def dfs(node, visited): visited.add(node) count = 1 for neighbor in graph[node]: if neighbor not in visited: count += dfs(neighbor, visited) return count max_bombs = 0 # Run DFS from every node for i in range(n): visited = set() max_bombs = max(max_bombs, dfs(i, visited)) return max_bombs
Let be the number of bombs.
Time Complexity:
Space Complexity:
The Graph DFS pattern used in LeetCode 2101 is highly versatile. It appears in various forms across many interview problems:
While Detonate the Maximum Bombs requires traversing from every node due to its directed nature, the core logic of using recursion or a stack to explore a graph remains identical.
Treating the graph as Undirected:
Using sqrt for distance checks:
Math.sqrt() or ** 0.5.Integer Overflow:
intlong (Java/C++) or Python (handles large ints automatically).Running DFS only once: