Detonate the Maximum Bombs Problem

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.

Examples

Example 1:

1
2
3
4
5
6
7
8
9
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:

1
2
3
4
5
6
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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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

Method 1 – Depth-First Search (DFS) on Bomb Detonation Graph

Intuition

We can model the bombs and their detonation ranges as a directed graph, where an edge from bomb i to bomb j exists if bomb j is within the range of bomb i. By running DFS from each bomb, we can find the maximum number of bombs that can be detonated starting from any single bomb.

Approach

  1. Build a directed graph where each node represents a bomb, and there is an edge from bomb i to bomb j if j is within the range of i.
  2. For each bomb, perform DFS to count how many bombs can be detonated starting from it.
  3. Track and return the maximum count found.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <vector>
#include <cmath>
using namespace std;
class Solution {
public:
	int maximumDetonation(vector<vector<int>>& bombs) {
		int n = bombs.size(), res = 0;
		vector<vector<int>> g(n);
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				if (i == j) continue;
				long long dx = bombs[j][0] - bombs[i][0];
				long long dy = bombs[j][1] - bombs[i][1];
				long long r = bombs[i][2];
				if (dx*dx + dy*dy <= r*r) g[i].push_back(j);
			}
		}
		function<int(int, vector<bool>&)> dfs = [&](int u, vector<bool>& vis) {
			vis[u] = true; int cnt = 1;
			for (int v : g[u]) if (!vis[v]) cnt += dfs(v, vis);
			return cnt;
		};
		for (int i = 0; i < n; ++i) {
			vector<bool> vis(n);
			res = max(res, dfs(i, vis));
		}
		return res;
	}
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
func maximumDetonation(bombs [][]int) int {
	n, res := len(bombs), 0
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if i == j { continue }
			dx, dy := bombs[j][0]-bombs[i][0], bombs[j][1]-bombs[i][1]
			r := bombs[i][2]
			if dx*dx+dy*dy <= r*r {
				g[i] = append(g[i], j)
			}
		}
	}
	var dfs func(int, []bool) int
	dfs = func(u int, vis []bool) int {
		vis[u] = true; cnt := 1
		for _, v := range g[u] {
			if !vis[v] { cnt += dfs(v, vis) }
		}
		return cnt
	}
	for i := 0; i < n; i++ {
		vis := make([]bool, n)
		if c := dfs(i, vis); c > res { res = c }
	}
	return res
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.*;
class Solution {
	public int maximumDetonation(int[][] bombs) {
		int n = bombs.length, res = 0;
		List<List<Integer>> g = new ArrayList<>();
		for (int i = 0; i < n; i++) g.add(new ArrayList<>());
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (i == j) continue;
				long dx = bombs[j][0] - bombs[i][0];
				long dy = bombs[j][1] - bombs[i][1];
				long r = bombs[i][2];
				if (dx*dx + dy*dy <= r*r) g.get(i).add(j);
			}
		}
		for (int i = 0; i < n; i++) {
			boolean[] vis = new boolean[n];
			res = Math.max(res, dfs(g, i, vis));
		}
		return res;
	}
	private int dfs(List<List<Integer>> g, int u, boolean[] vis) {
		vis[u] = true; int cnt = 1;
		for (int v : g.get(u)) if (!vis[v]) cnt += dfs(g, v, vis);
		return cnt;
	}
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
	fun maximumDetonation(bombs: Array<IntArray>): Int {
		val n = bombs.size
		val g = List(n) { mutableListOf<Int>() }
		for (i in 0 until n) {
			for (j in 0 until n) {
				if (i == j) continue
				val dx = bombs[j][0] - bombs[i][0]
				val dy = bombs[j][1] - bombs[i][1]
				val r = bombs[i][2].toLong()
				if (dx.toLong()*dx + dy.toLong()*dy <= r*r) g[i].add(j)
			}
		}
		fun dfs(u: Int, vis: BooleanArray): Int {
			vis[u] = true; var cnt = 1
			for (v in g[u]) if (!vis[v]) cnt += dfs(v, vis)
			return cnt
		}
		var res = 0
		for (i in 0 until n) {
			val vis = BooleanArray(n)
			res = maxOf(res, dfs(i, vis))
		}
		return res
	}
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
	def maximumDetonation(self, bombs: list[list[int]]) -> int:
		n = len(bombs)
		g = [[] for _ in range(n)]
		for i in range(n):
			for j in range(n):
				if i == j: continue
				dx = bombs[j][0] - bombs[i][0]
				dy = bombs[j][1] - bombs[i][1]
				r = bombs[i][2]
				if dx*dx + dy*dy <= r*r:
					g[i].append(j)
		def dfs(u, vis):
			vis[u] = True
			cnt = 1
			for v in g[u]:
				if not vis[v]:
					cnt += dfs(v, vis)
			return cnt
		res = 0
		for i in range(n):
			vis = [False]*n
			res = max(res, dfs(i, vis))
		return res
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
impl Solution {
	pub fn maximum_detonation(bombs: Vec<Vec<i32>>) -> i32 {
		let n = bombs.len();
		let mut g = vec![vec![]; n];
		for i in 0..n {
			for j in 0..n {
				if i == j { continue; }
				let dx = bombs[j][0] as i64 - bombs[i][0] as i64;
				let dy = bombs[j][1] as i64 - bombs[i][1] as i64;
				let r = bombs[i][2] as i64;
				if dx*dx + dy*dy <= r*r { g[i].push(j); }
			}
		}
		fn dfs(u: usize, g: &Vec<Vec<usize>>, vis: &mut Vec<bool>) -> i32 {
			vis[u] = true; let mut cnt = 1;
			for &v in &g[u] { if !vis[v] { cnt += dfs(v, g, vis); } }
			cnt
		}
		let mut res = 0;
		for i in 0..n {
			let mut vis = vec![false; n];
			res = res.max(dfs(i, &g, &mut vis));
		}
		res
	}
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function maximumDetonation(bombs: number[][]): number {
	const n = bombs.length;
	const g: number[][] = Array.from({length: n}, () => []);
	for (let i = 0; i < n; i++) {
		for (let j = 0; j < n; j++) {
			if (i === j) continue;
			const dx = bombs[j][0] - bombs[i][0];
			const dy = bombs[j][1] - bombs[i][1];
			const r = bombs[i][2];
			if (dx*dx + dy*dy <= r*r) g[i].push(j);
		}
	}
	function dfs(u: number, vis: boolean[]): number {
		vis[u] = true; let cnt = 1;
		for (const v of g[u]) if (!vis[v]) cnt += dfs(v, vis);
		return cnt;
	}
	let res = 0;
	for (let i = 0; i < n; i++) {
		const vis = Array(n).fill(false);
		res = Math.max(res, dfs(i, vis));
	}
	return res;
}

Complexity

  • ⏰ Time complexity: O(n^3) where n is the number of bombs (for graph construction and DFS from each node).
  • 🧺 Space complexity: O(n^2) for the adjacency list and visited arrays.