Medium
,Array
,Math
,DFS
,BFS
,Graph
2101. Detonate the Maximum Bombs
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.
Example 1:
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:
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:
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:
bombs.length
<= 100bombs[i].length
== 3xi
, yi
, ri
<= 105
using LL = long long;
class Solution {
public:
int maximumDetonation(vector<vector<int>>& bombs) {
int n = bombs.size();
vector<vector<int>> adjacent(n);
for (int i = 0; i < n; i ++) {
LL x_i = bombs[i][0];
LL y_i = bombs[i][1];
LL r_i = bombs[i][2];
for (int j = 0; j < n; j ++) {
if (i == j) {
continue;
}
LL x_j = bombs[j][0];
LL y_j = bombs[j][1];
LL r_j = bombs[j][2];
LL x_diff = x_i - x_j;
LL y_diff = y_i - y_j;
if (x_diff * x_diff + y_diff * y_diff <= r_i * r_i) {
adjacent[i].push_back(j);
}
}
}
int ans = 0;
for (int i = 0; i < n; i ++) {
ans = max(ans, numDetonation(i, n, adjacent));
}
return ans;
}
int numDetonation(int source, int n, vector<vector<int>>& adjacent) {
queue<int> frontier;
frontier.push(source);
vector<bool> visited(n, false);
visited[source] = true;
int distance = 1;
int numBomb = 1;
while (not frontier.empty()) {
const int u = frontier.front();
frontier.pop();
for (const int& v : adjacent[u]) {
if (visited[v]) {
continue;
}
frontier.push(v);
visited[v] = true;
numBomb ++;
distance = max(distance, numBomb);
}
}
return distance;
}
};
Jerry Wu2 June, 2023
class Solution:
def maximumDetonation(self, bombs: List[List[int]]) -> int:
ans = 0
graph = defaultdict(list)
for i, (xi, yi, ri) in enumerate(bombs):
for j, (xj, yj, rj) in enumerate(bombs):
if i == j: continue
if ri**2 >= (xi - xj)**2 + (yi - yj)**2:
graph[i].append(j)
def dfs(node, visited):
for child in graph[node]:
if child not in visited:
visited.add(child)
dfs(child, visited)
for i in range(len(bombs)):
visited = {i}
dfs(i, visited)
ans = max(ans, len(visited))
return ans
Yen-Chi ChenFri, Jun 2, 2023
public class Solution {
public int MaximumDetonation(int[][] bombs) {
if (bombs.Length == 1) return 1;
var graph = new Graph(bombs.Length);
SetGraph(bombs, graph);
int maxDetonation = 0;
for (int i = 0; i < bombs.Length; i++) {
int depth = graph.DFS(i);
maxDetonation = Math.Max(maxDetonation, depth);
}
return maxDetonation;
}
private void SetGraph(int[][] bombs, Graph graph) {
for (int i = 0; i < bombs.Length; i++) {
var bi = new Bomb(bombs[i]);
for (int j = i + 1; j < bombs.Length; j++) {
var bj = new Bomb(bombs[j]);
if (bi.CanDetonation(bj)) {
graph.AddEdge(i, j);
}
if (bj.CanDetonation(bi)) {
graph.AddEdge(j, i);
}
}
}
}
class Bomb {
public Bomb(params int[] param) {
X = param[0];
Y = param[1];
Radius = param[2];
}
public int X { get; set; }
public int Y { get; set; }
public int Radius { get; set; }
public bool CanDetonation(Bomb bomb) {
long x = bomb.X - this.X;
long y = bomb.Y - this.Y;
return x * x + y * y <= (long)Radius * Radius;
}
}
class Graph {
private readonly int _vertexCount;
private readonly HashSet<int>[] _adjaceny;
public Graph(int vertices) {
_vertexCount = vertices;
_adjaceny = new HashSet<int>[vertices];
for (int i = 0; i < _vertexCount; i++) {
_adjaceny[i] = new HashSet<int>();
}
}
public void AddEdge(int from, int to) {
_adjaceny[from].Add(to);
}
public int DFS(int vertex) {
bool[] visited = new bool[_vertexCount];
DFS(vertex, visited);
return visited.Count(v => v);
}
private void DFS(int vertex, bool[] visited) {
visited[vertex] = true;
foreach (var adjVertex in _adjaceny[vertex]) {
if (!visited[adjVertex]) {
DFS(adjVertex, visited);
}
}
}
}
}
JimJun 4, 2023