Skip to content

Commit abac155

Browse files
committed
LC 2101. Detonate the Maximum Bombs (Rust DFS)
1 parent 2cad78a commit abac155

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
477477
| [2007. Find Original Array From Doubled Array][lc2007] | 🟠 Medium | [![python](res/py.png)][lc2007py] |
478478
| [2013. Detect Squares][lc2013] | 🟠 Medium | [![python](res/py.png)][lc2013py] |
479479
| [2095. Delete the Middle Node of a Linked List][lc2095] | 🟠 Medium | [![python](res/py.png)][lc2095py] |
480+
| [2101. Detonate the Maximum Bombs][lc2101] | 🟠 Medium | [![rust](res/rs.png)][lc2101rs] |
480481
| [2115. Find All Possible Recipes from Given Supplies][lc2115] | 🟠 Medium | [![python](res/py.png)][lc2115py] |
481482
| [2127. Maximum Employees to Be Invited to a Meeting][lc2127] | 🔴 Hard | [![rust](res/rs.png)][lc2127rs] |
482483
| [2130. Maximum Twin Sum of a Linked List][lc2130] | 🟠 Medium | [![python](res/py.png)][lc2130py] [![rust](res/rs.png)][lc2130rs] |
@@ -1562,6 +1563,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
15621563
[lc2013py]: leetcode/detect-squares.py
15631564
[lc2095]: https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/
15641565
[lc2095py]: leetcode/delete-the-middle-node-of-a-linked-list.py
1566+
[lc2101]: https://leetcode.com/problems/detonate-the-maximum-bombs/
1567+
[lc2101rs]: leetcode/detonate-the-maximum-bombs.rs
15651568
[lc2115]: https://leetcode.com/problems/find-all-possible-recipes-from-given-supplies/
15661569
[lc2115py]: leetcode/find-all-possible-recipes-from-given-supplies.py
15671570
[lc2127]: https://leetcode.com/problems/maximum-employees-to-be-invited-to-a-meeting/
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// 2101. Detonate the Maximum Bombs
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/detonate-the-maximum-bombs/
5+
//
6+
// Tags: Array - Math - Depth-First Search - Breadth-First Search - Graph - Geometry
7+
8+
struct Solution {}
9+
impl Solution {
10+
/// The bombs form a directed graph where they are the nodes and the edges
11+
/// are formed between them and any other bombs that are within the radius
12+
/// of their explosions and will be triggered in a chain reaction. We can
13+
/// start by constructing an adjacency list using O(n^2) then we can do a
14+
/// graph traversal starting at each node checking how many bombs will be
15+
/// triggered and return the maximum result.
16+
///
17+
/// Time complexity: O(n^3) - We perform a DFS that could have a time
18+
/// complexity of O(n^2) for each node in the graph.
19+
/// Space complexity: O(n^2) - The adjacency 2D vector.
20+
///
21+
/// Runtime 18 ms Beats 100%
22+
/// Memory 2.2 MB Beats 100%
23+
pub fn maximum_detonation(bombs: Vec<Vec<i32>>) -> i32 {
24+
let n = bombs.len();
25+
let mut res = 0;
26+
// Create an adjacency vector.
27+
let mut adj: Vec<Vec<usize>> = vec![vec![]; n];
28+
// Iterate over all pairs.
29+
for i in 0..n {
30+
let (x1, y1, r1) = (bombs[i][0], bombs[i][1], bombs[i][2]);
31+
for j in i + 1..n {
32+
let (x2, y2, r2) = (bombs[j][0], bombs[j][1], bombs[j][2]);
33+
let dist = i64::pow((x1 - x2) as i64, 2) + i64::pow((y1 - y2) as i64, 2);
34+
if dist <= i64::pow(r1 as i64, 2) {
35+
// We can reach 2 from 1.
36+
adj[i].push(j);
37+
}
38+
if dist <= i64::pow(r2 as i64, 2) {
39+
adj[j].push(i);
40+
}
41+
}
42+
}
43+
fn dfs(i: usize, in_path: &mut Vec<bool>, adj: &Vec<Vec<usize>>) -> i32 {
44+
let mut count = 1;
45+
for nei in adj[i].iter() {
46+
let nei = *nei;
47+
if !in_path[nei] {
48+
in_path[nei] = true;
49+
count += dfs(nei, in_path, adj);
50+
}
51+
}
52+
count
53+
}
54+
let mut dp = vec![-1; n];
55+
// Pick start nodes to do a
56+
for i in 0..n {
57+
let mut in_path = vec![false; n];
58+
in_path[i] = true;
59+
dp[i] = dfs(i, &mut in_path, &adj);
60+
if dp[i] > res {
61+
res = dp[i];
62+
}
63+
}
64+
res
65+
}
66+
}
67+
68+
fn main() {
69+
let tests = [
70+
(vec![vec![2, 1, 3], vec![6, 1, 4]], 2),
71+
(vec![vec![1, 1, 5], vec![10, 10, 5]], 1),
72+
];
73+
for t in tests {
74+
assert_eq!(Solution::maximum_detonation(t.0), t.1);
75+
}
76+
println!("\x1b[92m» All tests passed!\x1b[0m")
77+
}

0 commit comments

Comments
 (0)