forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2101-detonate-the-maximum-bombs.kt
37 lines (34 loc) · 1.05 KB
/
2101-detonate-the-maximum-bombs.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
fun maximumDetonation(b: Array<IntArray>): Int {
val adj = HashMap<Int, ArrayList<Int>>().apply {
for (i in b.indices) {
for (j in b.indices) {
if (i != j) {
val dX = b[i][0].toDouble() - b[j][0].toDouble()
val dY = b[i][1].toDouble() - b[j][1].toDouble()
if ((b[i][2].toDouble() * b[i][2].toDouble()) >= (dX * dX + dY * dY))
this[i] = getOrDefault(i, ArrayList<Int>()).apply { add(j) }
}
}
}
}
fun dfs(i: Int, visit: HashSet<Int>) {
if (i in visit)
return
visit.add(i)
adj[i]?.forEach {
dfs(it, visit)
}
}
var res = 0
for (i in b.indices) {
val visit = HashSet<Int>()
dfs(i, visit)
res = maxOf(
res,
visit.size
)
}
return res
}
}