Detonate the Maximum Bombs - Graph & BFS/DFS [JS]
- Get link
- X
- Other Apps
By
Anna An
on
Solution 1: Graph & DFS
- Build a directed graph for all the bombs,
bombs[i]
is connected tobombs[j]
if the distance between the two points are smaller than or equal to the radius ofbombs[i]
. - Recursively DFS from each bomb and record the maximum number of points reached from each bomb.
Time Complexity: O(n^2)
370ms
Space Complexity: O(n^2)
50.7MB
var maximumDetonation = function(bombs) {
let n = bombs.length, graph = Array(n).fill(0).map(() => []);
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (i === j) continue;
let [x1, y1, radius] = bombs[i], [x2, y2] = bombs[j];
if (getDist([x1, y1], [x2, y2]) <= radius) {
graph[i].push(j);
}
}
}
let ans = 0;
for (let i = 0; i < n; i++) {
ans = Math.max(ans, dfs(i, Array(n).fill(0)));
}
return ans;
function dfs(node, seen) {
if (seen[node]) return 0;
seen[node] = 1;
if (graph[node].length === 0) return 1;
let ans = 0;
for (let nei of graph[node]) {
ans += dfs(nei, seen);
}
return ans + 1;
}
function getDist(p1, p2) {
let [x1, y1] = p1, [x2, y2] = p2;
let calc1 = (x2 - x1) * (x2 - x1), calc2 = (y2 - y1) * (y2 - y1);
return Math.sqrt(calc1 + calc2);
}
};
Solution 2: Graph & BFS
The same as solution 1, except using BFS instead of DFS.
Time Complexity: O(n^2)
385ms
Space Complexity: O(n^2)
51.9MB
var maximumDetonation = function(bombs) {
let n = bombs.length, graph = Array(n).fill(0).map(() => []);
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (i === j) continue;
let [x1, y1, radius] = bombs[i], [x2, y2] = bombs[j];
if (getDist([x1, y1], [x2, y2]) <= radius) {
graph[i].push(j);
}
}
}
let ans = 0;
for (let i = 0; i < n; i++) {
ans = Math.max(ans, bfs(i));
}
return ans;
function bfs(node) {
let seen = Array(n).fill(0), queue = [node], count = 0;
seen[node] = 1;
while (queue.length) {
let node = queue.shift();
count++;
for (let nei of graph[node]) {
if (seen[nei]) continue;
queue.push(nei);
seen[nei] = 1;
}
}
return count;
}
function getDist(p1, p2) {
let [x1, y1] = p1, [x2, y2] = p2;
let calc1 = (x2 - x1) * (x2 - x1), calc2 = (y2 - y1) * (y2 - y1);
return Math.sqrt(calc1 + calc2);
}
};
javascript
- Get link
- X
- Other Apps
Comments
Post a Comment