-
Notifications
You must be signed in to change notification settings - Fork 0
/
Detonate the Maximum Bombs.java
60 lines (39 loc) · 1.27 KB
/
Detonate the Maximum Bombs.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
class Solution {
public int maximumDetonation(int[][] bombs) {
ArrayList<Integer>[] arr=new ArrayList[bombs.length];
for(int i=0;i<bombs.length;i++){
arr[i]=new ArrayList<Integer>();
}
for(int i=0;i<bombs.length;i++){
int r1=bombs[i][2];
int x1=bombs[i][0];
int y1=bombs[i][1];
for(int j=0;j<bombs.length;j++){
int r2=bombs[j][2];
int x2=bombs[j][0];
int y2=bombs[j][1];
if(i!=j){
if(Math.pow(x1 - x2, 2) + Math.pow(y1 -y2, 2) <= Math.pow(r1, 2)){
arr[i].add(j);
}
}
}
}
int max=0;
for(int i=0;i<bombs.length;i++){
boolean[] vis=new boolean[bombs.length];
max=Math.max(dfs(arr,vis,i,1),max);
}
return max;
}
public int dfs(ArrayList<Integer>[] arr,boolean[] vis,int index,int num){
vis[index]=true;
for(int i=0;i<arr[index].size();i++){
int x=arr[index].get(i);
if(!vis[x]){
num=Math.max(dfs(arr,vis,x,num+1),num);
}
}
return num;
}
}