-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2101-detonate-the-maximum-bombs.py
More file actions
36 lines (29 loc) · 984 Bytes
/
2101-detonate-the-maximum-bombs.py
File metadata and controls
36 lines (29 loc) · 984 Bytes
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
# time complexity: O(n^3)
# space complexity: O(n^2)
from collections import defaultdict
from typing import List
class Solution:
def maximumDetonation(self, bombs: List[List[int]]) -> int:
result = 0
graph = defaultdict(list)
n = len(bombs)
for i in range(n):
for j in range(n):
if i == j:
continue
xi, yi, r = bombs[i]
xj, yj, _ = bombs[j]
if r**2 >= (xi-xj) ** 2 + (yi - yj) ** 2:
graph[i].append(j)
def dfs(curr, visited):
visited.add(curr)
for neighbor in graph[curr]:
if neighbor not in visited:
dfs(neighbor, visited)
return len(visited)
for i in range(n):
visited = set()
result = max(result, dfs(i, visited))
return result
bombs = [[2, 1, 3], [6, 1, 4]]
print(Solution().maximumDetonation(bombs))