-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.6.23(2101. Detonate the Maximum Bombs(MEDIUM))
61 lines (54 loc) · 1.43 KB
/
2.6.23(2101. Detonate the Maximum Bombs(MEDIUM))
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
TC : O(N^3)
SC : O(N^2)
-------------------------------------------------------------------------------------------
class Solution {
void dfs(vector<int>adj[],vector<int>&vis,int node,int& cnt)
{
vis[node] = 1;
cnt++;
for(auto nd : adj[node])
{
if(!vis[nd])
{
dfs(adj,vis,nd,cnt);
}
}
}
public:
int maximumDetonation(vector<vector<int>>& bombs)
{
int n = bombs.size();
vector<int>adj[n];
//make the adjacency list where each node keeps track of the node which it can detonate
for(int i = 0; i < n; i++)
{
int x1 = bombs[i][0];
int y1 = bombs[i][1];
int r1 = bombs[i][2];
for(int j = 0; j < n; j++)
{
if(i != j)
{
int x2 = bombs[j][0];
int y2 = bombs[j][1];
//it is the dstance between two nodes
double dist = sqrt(1ll*(x2-x1)*(x2-x1) + 1ll*(y2-y1)*(y2-y1));
if(r1 >= dist)
{
adj[i].push_back(j);
}
}
}
}
//go for a dfs call
int ans = 0;
for(int i = 0; i < n; i++)
{
vector<int>vis(n,0);
int cnt = 0;
dfs(adj,vis,i,cnt);
ans = max(ans,cnt);
}
return ans;
}
};