Skip to content

Commit 0d36fd7

Browse files
committed
Leetcode | Detonate the Maximum Bombs | Kotlin
1 parent a2b7014 commit 0d36fd7

3 files changed

Lines changed: 141 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# 2101. Detonate the Maximum Bombs
2+
3+
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
4+
the shape of a circle with the center as the location of the bomb.
5+
6+
The bombs are represented by a 0-indexed 2D integer array `bombs` where `bombs[i] = [xi, yi, ri]`. `xi` and `yi` denote the
7+
X-coordinate and Y-coordinate of the location of the `ith` bomb, whereas `ri` denotes the radius of its range.
8+
9+
You may choose to detonate a single bomb. When a bomb is detonated, it will detonate all bombs that lie in its range.
10+
These bombs will further detonate the bombs that lie in their ranges.
11+
12+
Given the list of bombs, return the maximum number of bombs that can be detonated if you are allowed to detonate only
13+
one bomb.
14+
15+
Example 1:
16+
17+
Input: bombs = [[2,1,3],[6,1,4]]
18+
Output: 2
19+
Explanation:
20+
The above figure shows the positions and ranges of the 2 bombs.
21+
If we detonate the left bomb, the right bomb will not be affected.
22+
But if we detonate the right bomb, both bombs will be detonated.
23+
So the maximum bombs that can be detonated is max(1, 2) = 2.
24+
25+
Example 2:
26+
27+
Input: bombs = [[1,1,5],[10,10,5]]
28+
Output: 1
29+
Explanation:
30+
Detonating either bomb will not detonate the other bomb, so the maximum number of bombs that can be detonated is 1.
31+
32+
Example 3:
33+
34+
Input: bombs = [[1,2,3],[2,3,1],[3,4,2],[4,5,3],[5,6,4]]
35+
Output: 5
36+
Explanation:
37+
The best bomb to detonate is bomb 0 because:
38+
39+
- Bomb 0 detonates bombs 1 and 2. The red circle denotes the range of bomb 0.
40+
- Bomb 2 detonates bomb 3. The blue circle denotes the range of bomb 2.
41+
- Bomb 3 detonates bomb 4. The green circle denotes the range of bomb 3.
42+
Thus all 5 bombs are detonated.
43+
44+
Constraints:
45+
46+
- `1 <= bombs.length <= 100`
47+
- `bombs[i].length == 3`
48+
- `1 <= xi, yi, ri <= 10^5`
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.devstromo.medium.p2101
2+
3+
class SolutionKt {
4+
fun maximumDetonation(bombs: Array<IntArray>): Int {
5+
val n = bombs.size
6+
val graph = Array(n) { mutableListOf<Int>() }
7+
8+
// Build graph: directed edge from i to j if j is in i's blast radius
9+
for (i in 0 until n) {
10+
val (xi, yi, ri) = bombs[i]
11+
for (j in 0 until n) {
12+
if (i == j) continue
13+
val (xj, yj) = bombs[j]
14+
val dx = xi.toLong() - xj
15+
val dy = yi.toLong() - yj
16+
val r = ri.toLong()
17+
18+
if (dx * dx + dy * dy <= r * r) {
19+
graph[i].add(j)
20+
}
21+
}
22+
}
23+
24+
var max = 0
25+
for (i in 0 until n) {
26+
val visited = BooleanArray(n)
27+
val count = dfs(i, graph, visited)
28+
max = maxOf(max, count)
29+
}
30+
31+
return max
32+
}
33+
34+
private fun dfs(current: Int, graph: Array<MutableList<Int>>, visited: BooleanArray): Int {
35+
visited[current] = true
36+
var count = 1
37+
for (neighbor in graph[current]) {
38+
if (!visited[neighbor]) {
39+
count += dfs(neighbor, graph, visited)
40+
}
41+
}
42+
return count
43+
}
44+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.devstromo.medium.p2101
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
6+
class SolutionKtUnitTest {
7+
private val solution = SolutionKt()
8+
9+
@Test
10+
fun `Test Detonate the Maximum Bombs`() {
11+
assertEquals(
12+
2, solution.maximumDetonation(
13+
arrayOf(
14+
intArrayOf(2, 1, 3),
15+
intArrayOf(6, 1, 4),
16+
)
17+
)
18+
)
19+
assertEquals(
20+
1, solution.maximumDetonation(
21+
arrayOf(
22+
intArrayOf(1, 1, 5),
23+
intArrayOf(10, 10, 5),
24+
)
25+
)
26+
)
27+
assertEquals(
28+
5, solution.maximumDetonation(
29+
arrayOf(
30+
intArrayOf(1, 2, 3),
31+
intArrayOf(2, 3, 1),
32+
intArrayOf(3, 4, 2),
33+
intArrayOf(4, 5, 3),
34+
intArrayOf(5, 6, 4)
35+
)
36+
)
37+
)
38+
39+
assertEquals(
40+
1,
41+
solution.maximumDetonation(
42+
arrayOf(
43+
intArrayOf(1, 1, 100000),
44+
intArrayOf(100000, 100000, 1)
45+
)
46+
)
47+
)
48+
}
49+
}

0 commit comments

Comments
 (0)