-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2101DetonateTheMaximumBombs.java
More file actions
66 lines (55 loc) · 1.96 KB
/
2101DetonateTheMaximumBombs.java
File metadata and controls
66 lines (55 loc) · 1.96 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//https://leetcode.com/problems/detonate-the-maximum-bombs/
class Solution {
boolean isReachable(int[] a, int[] b) {
long dx = (long)a[0] - (long)b[0], dy = (long)a[1] - (long)b[1], r = (long)a[2];
return dx * dx + dy * dy <= r * r;
}
public int maximumDetonation(int[][] bombs) {
List<List<Integer>> graph = new ArrayList<>();
for(int i = 0; i < bombs.length; i++) {
graph.add(new ArrayList<>());
}
for(int i = 0; i < bombs.length; i++) {
for(int j = 0; j < bombs.length; j++) {
if(i != j && isReachable(bombs[i], bombs[j])) {
graph.get(i).add(j);
}
}
}
int maxBomb = 1;
for(int i = 0; i < bombs.length; i++) {
boolean[] visited = new boolean[bombs.length];
//maxBomb = Math.max(maxBomb, dfs(i, graph, visited));
maxBomb = Math.max(maxBomb, bfs(i, graph, visited));
}
return maxBomb;
}
int bfs(int source, List<List<Integer>> graph, boolean[] visited) {
Queue<Integer> queue = new LinkedList();
queue.offer(source);
visited[source] = true;
int count = 0;
while(!queue.isEmpty()) {
Integer top = queue.poll();
count++;
for(Integer neighbour : graph.get(top)) {
if(!visited[neighbour]) {
visited[neighbour] = true;
queue.offer(neighbour);
}
}
}
return count;
}
int dfs(Integer point, List<List<Integer>> graph, boolean[] visited) {
if(visited[point]) {
return 0;
}
visited[point] = true;
int currentValue = 1;
for(Integer neighbour : graph.get(point)) {
currentValue += dfs(neighbour, graph, visited);
}
return currentValue;
}
}