Skip to main content
Back to problems
#2101
Medium Algorithms

Detonate the maximum bombs

Array Math Depth-First Search Breadth-First Search Graph Theory Geometry
49.9% acceptance
Feb 25, 2026
3368
161
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.

Solution

Rust
Time O(n * m)
Space O(n * m)
LeetCode
solution.rs
impl Solution {
  pub fn maximum_detonation(bombs: Vec<Vec<i32>>) -> i32 {
    let n = bombs.len();
    // Build adjacency: i -> j if bomb i can detonate bomb j
    let mut adj = vec![vec![]; n];
    for i in 0..n {
      let (xi, yi, ri) = (bombs[i][0] as i64, bombs[i][1] as i64, bombs[i][2] as i64);
      for j in 0..n {
        if i == j { continue; }
        let (xj, yj) = (bombs[j][0] as i64, bombs[j][1] as i64);
        let dx = xi - xj;
        let dy = yi - yj;
        if dx * dx + dy * dy <= ri * ri {
          adj[i].push(j);
        }
      }
    }
    let mut best = 0;
    for start in 0..n {
      let mut visited = vec![false; n];
      let mut stack = vec![start];
      visited[start] = true;
      let mut count = 0;
      while let Some(node) = stack.pop() {
        count += 1;
        for &nb in &adj[node] {
          if !visited[nb] {
            visited[nb] = true;
            stack.push(nb);
          }
        }
      }
      best = best.max(count);
    }
    best
  }
}