-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxBombs.java
66 lines (54 loc) · 1.66 KB
/
MaxBombs.java
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
package org.sean.graph;
import java.util.*;
/***
* 2101. Detonate the Maximum Bombs
*/
public class MaxBombs {
//
// Edges in the graph could be up to N^2.
// Time complexity O(N^3)
//
public int maximumDetonation(int[][] bombs) {
int len = bombs.length;
if (len == 1)
return 1;
boolean[] visited = new boolean[len];
List<List<Integer>> graph = new ArrayList<>();
for (int n = 0; n < len; n++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
if (i != j) {
if (included(bombs[i], bombs[j])) {
graph.get(i).add(j);
}
}
}
}
// detonate one bomb : check the max reachable booms
int max = 0;
for (int i = 0; i < len; i++) {
int res = dfs(graph, i, visited);
max = Math.max(max, res);
Arrays.fill(visited, false);
}
return max;
}
private boolean included(int[] circle1, int[] circle2) {
// also consider the possible data precision loss
double dist =
Math.pow(circle1[0] - circle2[0], 2) + Math.pow(circle1[1] - circle2[1], 2);
return 1.0f * circle1[2] * circle1[2] >= dist;
}
private int dfs(List<List<Integer>> graph, int pos, boolean[] visited) {
int cnt = 1;
visited[pos] = true;
for (int edge : graph.get(pos)) {
if (!visited[edge]) {
cnt += dfs(graph, edge, visited);
}
}
return cnt;
}
}