Skip to content

Commit a9518fb

Browse files
authored
Create 2101-detonate-the-maximum-bombs.kt
1 parent cd40ef7 commit a9518fb

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
fun maximumDetonation(b: Array<IntArray>): Int {
3+
val adj = HashMap<Int, ArrayList<Int>>().apply {
4+
for (i in b.indices) {
5+
for (j in b.indices) {
6+
if (i != j) {
7+
val dX = b[i][0].toDouble() - b[j][0].toDouble()
8+
val dY = b[i][1].toDouble() - b[j][1].toDouble()
9+
if ((b[i][2].toDouble() * b[i][2].toDouble()) >= (dX * dX + dY * dY))
10+
this[i] = getOrDefault(i, ArrayList<Int>()).apply { add(j) }
11+
}
12+
}
13+
}
14+
}
15+
16+
fun dfs(i: Int, visit: HashSet<Int>) {
17+
if (i in visit)
18+
return
19+
visit.add(i)
20+
adj[i]?.forEach {
21+
dfs(it, visit)
22+
}
23+
}
24+
25+
var res = 0
26+
for (i in b.indices) {
27+
val visit = HashSet<Int>()
28+
dfs(i, visit)
29+
res = maxOf(
30+
res,
31+
visit.size
32+
)
33+
}
34+
35+
return res
36+
}
37+
}

0 commit comments

Comments
 (0)