Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7c1baa8

Browse files
authoredJun 22, 2024
feat: add solutions to lc problem: No.2101 (doocs#3147)
No.2101.Detonate the Maximum Bombs
1 parent 0df8e90 commit 7c1baa8

File tree

9 files changed

+329
-309
lines changed

9 files changed

+329
-309
lines changed
 

‎.prettierignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ node_modules/
1515
/solution/bash_problem_readme_template_en.md
1616
/solution/0100-0199/0177.Nth Highest Salary/Solution.sql
1717
/solution/0100-0199/0178.Rank Scores/Solution2.sql
18-
/solution/0100-0199/0196.Delete Duplicate Emails/Solution3.sql
1918
/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/Solution2.sql
2019
/solution/1400-1499/1454.Active Users/Solution.sql
2120
/solution/1600-1699/1635.Hopper Company Queries I/Solution.sql

‎.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"braceStyle": "1tbs",
1212
"endOfLine": "lf",
1313
"sqlKeywordCase": "upper",
14+
"sqlCanonicalSyntax": false,
1415
"overrides": [
1516
{
1617
"files": ["*.sql"],

‎solution/2100-2199/2101.Detonate the Maximum Bombs/README.md

Lines changed: 111 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ tags:
8888

8989
### 方法一:BFS
9090

91-
枚举每个炸弹 k 作为起始引爆点,BFS 搜索能影响到的所有炸弹的数量,取其最大值。
91+
我们定义一个长度为 $n$ 的数组 $g$,其中 $g[i]$ 表示炸弹 $i$ 的爆炸范围内可以引爆的所有炸弹的下标。
92+
93+
然后,我们遍历所有炸弹,对于两个炸弹 $(x_1, y_1, r_1)$ 和 $(x_2, y_2, r_2)$,我们计算它们之间的距离 $\text{dist} = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}$。如果 $\text{dist} \leq r_1$,那么炸弹 $i$ 的爆炸范围内可以引爆炸弹 $j$,我们就将 $j$ 添加到 $g[i]$ 中。如果 $\text{dist} \leq r_2$,那么炸弹 $j$ 的爆炸范围内可以引爆炸弹 $i$,我们就将 $i$ 添加到 $g[j]$ 中。
94+
95+
接下来,我们遍历所有炸弹,对于每个炸弹 $k$,我们使用广度优先搜索计算炸弹 $k$ 的爆炸范围内可以引爆的所有炸弹的下标,并记录下来。如果这些炸弹的数量等于 $n$,那么我们就可以引爆所有炸弹,直接返回 $n$。否则,我们记录下来这些炸弹的数量,并返回最大值。
96+
97+
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 为炸弹的数量。
9298

9399
<!-- tabs:start -->
94100

@@ -97,82 +103,77 @@ tags:
97103
```python
98104
class Solution:
99105
def maximumDetonation(self, bombs: List[List[int]]) -> int:
100-
def check(i, j):
101-
if i == j:
102-
return False
103-
x, y = bombs[i][0] - bombs[j][0], bombs[i][1] - bombs[j][1]
104-
r = bombs[i][2]
105-
return r * r >= x * x + y * y
106-
107-
g = defaultdict(list)
108106
n = len(bombs)
109-
for i in range(n):
110-
for j in range(n):
111-
if check(i, j):
107+
g = [[] for _ in range(n)]
108+
for i in range(n - 1):
109+
x1, y1, r1 = bombs[i]
110+
for j in range(i + 1, n):
111+
x2, y2, r2 = bombs[j]
112+
dist = hypot(x1 - x2, y1 - y2)
113+
if dist <= r1:
112114
g[i].append(j)
115+
if dist <= r2:
116+
g[j].append(i)
113117
ans = 0
114118
for k in range(n):
115-
q = deque([k])
116-
vis = [False] * n
117-
vis[k] = True
118-
cnt = 0
119-
while q:
120-
i = q.popleft()
121-
cnt += 1
119+
vis = {k}
120+
q = [k]
121+
for i in q:
122122
for j in g[i]:
123-
if not vis[j]:
124-
vis[j] = True
123+
if j not in vis:
124+
vis.add(j)
125125
q.append(j)
126-
ans = max(ans, cnt)
126+
if len(vis) == n:
127+
return n
128+
ans = max(ans, len(vis))
127129
return ans
128130
```
129131

130132
#### Java
131133

132134
```java
133135
class Solution {
134-
private int[][] bombs;
135-
136136
public int maximumDetonation(int[][] bombs) {
137-
this.bombs = bombs;
138137
int n = bombs.length;
139-
boolean[][] g = new boolean[n][n];
140-
for (int i = 0; i < n; ++i) {
141-
for (int j = 0; j < n; ++j) {
142-
g[i][j] = check(i, j);
138+
List<Integer>[] g = new List[n];
139+
Arrays.setAll(g, k -> new ArrayList<>());
140+
for (int i = 0; i < n - 1; ++i) {
141+
for (int j = i + 1; j < n; ++j) {
142+
int[] p1 = bombs[i], p2 = bombs[j];
143+
double dist = Math.hypot(p1[0] - p2[0], p1[1] - p2[1]);
144+
if (dist <= p1[2]) {
145+
g[i].add(j);
146+
}
147+
if (dist <= p2[2]) {
148+
g[j].add(i);
149+
}
143150
}
144151
}
145152
int ans = 0;
153+
boolean[] vis = new boolean[n];
146154
for (int k = 0; k < n; ++k) {
147-
Deque<Integer> q = new ArrayDeque<>();
148-
q.offer(k);
149-
boolean[] vis = new boolean[n];
155+
Arrays.fill(vis, false);
150156
vis[k] = true;
151157
int cnt = 0;
158+
Deque<Integer> q = new ArrayDeque<>();
159+
q.offer(k);
152160
while (!q.isEmpty()) {
153161
int i = q.poll();
154162
++cnt;
155-
for (int j = 0; j < n; ++j) {
156-
if (g[i][j] && !vis[j]) {
163+
for (int j : g[i]) {
164+
if (!vis[j]) {
157165
vis[j] = true;
158166
q.offer(j);
159167
}
160168
}
161169
}
170+
if (cnt == n) {
171+
return n;
172+
}
162173
ans = Math.max(ans, cnt);
163174
}
164175
return ans;
165176
}
166-
167-
private boolean check(int i, int j) {
168-
if (i == j) {
169-
return false;
170-
}
171-
long x = bombs[i][0] - bombs[j][0];
172-
long y = bombs[i][1] - bombs[j][1];
173-
long r = bombs[i][2];
174-
return r * r >= x * x + y * y;
175-
}
176177
}
177178
```
178179

@@ -183,64 +184,67 @@ class Solution {
183184
public:
184185
int maximumDetonation(vector<vector<int>>& bombs) {
185186
int n = bombs.size();
186-
vector<vector<bool>> g(n, vector<bool>(n));
187-
for (int i = 0; i < n; ++i)
188-
for (int j = 0; j < n; ++j)
189-
g[i][j] = check(i, j, bombs);
187+
vector<int> g[n];
188+
for (int i = 0; i < n - 1; ++i) {
189+
for (int j = i + 1; j < n; ++j) {
190+
auto& p1 = bombs[i];
191+
auto& p2 = bombs[j];
192+
auto dist = hypot(p1[0] - p2[0], p1[1] - p2[1]);
193+
if (dist <= p1[2]) {
194+
g[i].push_back(j);
195+
}
196+
if (dist <= p2[2]) {
197+
g[j].push_back(i);
198+
}
199+
}
200+
}
190201
int ans = 0;
202+
bool vis[n];
191203
for (int k = 0; k < n; ++k) {
192-
queue<int> q{{k}};
193-
vector<bool> vis(n);
204+
memset(vis, false, sizeof(vis));
205+
queue<int> q;
206+
q.push(k);
194207
vis[k] = true;
195208
int cnt = 0;
196209
while (!q.empty()) {
197210
int i = q.front();
198211
q.pop();
199212
++cnt;
200-
for (int j = 0; j < n; ++j) {
201-
if (g[i][j] && !vis[j]) {
213+
for (int j : g[i]) {
214+
if (!vis[j]) {
202215
vis[j] = true;
203216
q.push(j);
204217
}
205218
}
206219
}
220+
if (cnt == n) {
221+
return n;
222+
}
207223
ans = max(ans, cnt);
208224
}
209225
return ans;
210226
}
211-
212-
bool check(int i, int j, vector<vector<int>>& bombs) {
213-
if (i == j) return false;
214-
long long x = bombs[i][0] - bombs[j][0];
215-
long long y = bombs[i][1] - bombs[j][1];
216-
long long r = bombs[i][2];
217-
return r * r >= x * x + y * y;
218-
}
219227
};
220228
```
221229
222230
#### Go
223231
224232
```go
225-
func maximumDetonation(bombs [][]int) int {
226-
check := func(i, j int) bool {
227-
if i == j {
228-
return false
229-
}
230-
x, y := bombs[i][0]-bombs[j][0], bombs[i][1]-bombs[j][1]
231-
r := bombs[i][2]
232-
return r*r >= x*x+y*y
233-
}
233+
func maximumDetonation(bombs [][]int) (ans int) {
234234
n := len(bombs)
235-
g := make([][]bool, n)
236-
for i := range g {
237-
g[i] = make([]bool, n)
238-
for j := range g[i] {
239-
g[i][j] = check(i, j)
235+
g := make([][]int, n)
236+
for i, p1 := range bombs[:n-1] {
237+
for j := i + 1; j < n; j++ {
238+
p2 := bombs[j]
239+
dist := math.Hypot(float64(p1[0]-p2[0]), float64(p1[1]-p2[1]))
240+
if dist <= float64(p1[2]) {
241+
g[i] = append(g[i], j)
242+
}
243+
if dist <= float64(p2[2]) {
244+
g[j] = append(g[j], i)
245+
}
240246
}
241247
}
242-
243-
ans := 0
244248
for k := 0; k < n; k++ {
245249
q := []int{k}
246250
vis := make([]bool, n)
@@ -250,16 +254,19 @@ func maximumDetonation(bombs [][]int) int {
250254
i := q[0]
251255
q = q[1:]
252256
cnt++
253-
for j := 0; j < n; j++ {
254-
if g[i][j] && !vis[j] {
257+
for _, j := range g[i] {
258+
if !vis[j] {
255259
vis[j] = true
256260
q = append(q, j)
257261
}
258262
}
259263
}
264+
if cnt == n {
265+
return n
266+
}
260267
ans = max(ans, cnt)
261268
}
262-
return ans
269+
return
263270
}
264271
```
265272

@@ -268,37 +275,38 @@ func maximumDetonation(bombs [][]int) int {
268275
```ts
269276
function maximumDetonation(bombs: number[][]): number {
270277
const n = bombs.length;
271-
const g = new Map<number, number[]>(bombs.map((_, i) => [i, []]));
272-
273-
for (let i = 0; i < n - 1; i++) {
274-
for (let j = 1; j < n; j++) {
275-
const [x1, y1, r1] = bombs[i];
278+
const g: number[][] = Array.from({ length: n }, () => []);
279+
for (let i = 0; i < n - 1; ++i) {
280+
const [x1, y1, r1] = bombs[i];
281+
for (let j = i + 1; j < n; ++j) {
276282
const [x2, y2, r2] = bombs[j];
277-
const distance = Math.hypot(x1 - x2, y1 - y2);
278-
279-
if (distance <= r1) g.get(i)!.push(j);
280-
if (distance <= r2) g.get(j)!.push(i);
283+
const d = Math.hypot(x1 - x2, y1 - y2);
284+
if (d <= r1) {
285+
g[i].push(j);
286+
}
287+
if (d <= r2) {
288+
g[j].push(i);
289+
}
281290
}
282291
}
283-
284-
let res = 0;
285-
for (let i = 0; i < n; i++) {
286-
const seen = new Set<number>([i]);
287-
const q = [i];
288-
292+
let ans = 0;
293+
for (let k = 0; k < n; ++k) {
294+
const vis: Set<number> = new Set([k]);
295+
const q: number[] = [k];
289296
for (const i of q) {
290-
for (const j of g.get(i) ?? []) {
291-
if (seen.has(j)) continue;
292-
seen.add(j);
293-
q.push(j);
297+
for (const j of g[i]) {
298+
if (!vis.has(j)) {
299+
vis.add(j);
300+
q.push(j);
301+
}
294302
}
295303
}
296-
297-
if (seen.size === n) return n;
298-
res = Math.max(res, seen.size);
304+
if (vis.size === n) {
305+
return n;
306+
}
307+
ans = Math.max(ans, vis.size);
299308
}
300-
301-
return res;
309+
return ans;
302310
}
303311
```
304312

‎solution/2100-2199/2101.Detonate the Maximum Bombs/README_EN.md

Lines changed: 113 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,15 @@ Thus all 5 bombs are detonated.
8181

8282
<!-- solution:start -->
8383

84-
### Solution 1
84+
### Solution 1: BFS
85+
86+
We define an array $g$ of length $n$, where $g[i]$ represents the indices of all bombs that can be triggered by bomb $i$ within its explosion range.
87+
88+
Next, we iterate over all bombs. For two bombs $(x_1, y_1, r_1)$ and $(x_2, y_2, r_2)$, we calculate the distance between them $\text{dist} = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}$. If $\text{dist} \leq r_1$, then bomb $i$ can trigger bomb $j$ within its explosion range, so we add $j$ to $g[i]$. If $\text{dist} \leq r_2$, then bomb $j$ can trigger bomb $i$ within its explosion range, so we add $i$ to $g[j]$.
89+
90+
Next, we iterate over all bombs. For each bomb $k$, we use breadth-first search to calculate the indices of all bombs that can be triggered by bomb $k$ within its explosion range and record them. If the number of these bombs equals $n$, then we can trigger all bombs and directly return $n$. Otherwise, we record the number of these bombs and return the maximum value.
91+
92+
The time complexity is $O(n^2)$ and the space complexity is $O(n^2)$, where $n$ is the number of bombs.
8593

8694
<!-- tabs:start -->
8795

@@ -90,82 +98,77 @@ Thus all 5 bombs are detonated.
9098
```python
9199
class Solution:
92100
def maximumDetonation(self, bombs: List[List[int]]) -> int:
93-
def check(i, j):
94-
if i == j:
95-
return False
96-
x, y = bombs[i][0] - bombs[j][0], bombs[i][1] - bombs[j][1]
97-
r = bombs[i][2]
98-
return r * r >= x * x + y * y
99-
100-
g = defaultdict(list)
101101
n = len(bombs)
102-
for i in range(n):
103-
for j in range(n):
104-
if check(i, j):
102+
g = [[] for _ in range(n)]
103+
for i in range(n - 1):
104+
x1, y1, r1 = bombs[i]
105+
for j in range(i + 1, n):
106+
x2, y2, r2 = bombs[j]
107+
dist = hypot(x1 - x2, y1 - y2)
108+
if dist <= r1:
105109
g[i].append(j)
110+
if dist <= r2:
111+
g[j].append(i)
106112
ans = 0
107113
for k in range(n):
108-
q = deque([k])
109-
vis = [False] * n
110-
vis[k] = True
111-
cnt = 0
112-
while q:
113-
i = q.popleft()
114-
cnt += 1
114+
vis = {k}
115+
q = [k]
116+
for i in q:
115117
for j in g[i]:
116-
if not vis[j]:
117-
vis[j] = True
118+
if j not in vis:
119+
vis.add(j)
118120
q.append(j)
119-
ans = max(ans, cnt)
121+
if len(vis) == n:
122+
return n
123+
ans = max(ans, len(vis))
120124
return ans
121125
```
122126

123127
#### Java
124128

125129
```java
126130
class Solution {
127-
private int[][] bombs;
128-
129131
public int maximumDetonation(int[][] bombs) {
130-
this.bombs = bombs;
131132
int n = bombs.length;
132-
boolean[][] g = new boolean[n][n];
133-
for (int i = 0; i < n; ++i) {
134-
for (int j = 0; j < n; ++j) {
135-
g[i][j] = check(i, j);
133+
List<Integer>[] g = new List[n];
134+
Arrays.setAll(g, k -> new ArrayList<>());
135+
for (int i = 0; i < n - 1; ++i) {
136+
for (int j = i + 1; j < n; ++j) {
137+
int[] p1 = bombs[i], p2 = bombs[j];
138+
double dist = Math.hypot(p1[0] - p2[0], p1[1] - p2[1]);
139+
if (dist <= p1[2]) {
140+
g[i].add(j);
141+
}
142+
if (dist <= p2[2]) {
143+
g[j].add(i);
144+
}
136145
}
137146
}
138147
int ans = 0;
148+
boolean[] vis = new boolean[n];
139149
for (int k = 0; k < n; ++k) {
140-
Deque<Integer> q = new ArrayDeque<>();
141-
q.offer(k);
142-
boolean[] vis = new boolean[n];
150+
Arrays.fill(vis, false);
143151
vis[k] = true;
144152
int cnt = 0;
153+
Deque<Integer> q = new ArrayDeque<>();
154+
q.offer(k);
145155
while (!q.isEmpty()) {
146156
int i = q.poll();
147157
++cnt;
148-
for (int j = 0; j < n; ++j) {
149-
if (g[i][j] && !vis[j]) {
158+
for (int j : g[i]) {
159+
if (!vis[j]) {
150160
vis[j] = true;
151161
q.offer(j);
152162
}
153163
}
154164
}
165+
if (cnt == n) {
166+
return n;
167+
}
155168
ans = Math.max(ans, cnt);
156169
}
157170
return ans;
158171
}
159-
160-
private boolean check(int i, int j) {
161-
if (i == j) {
162-
return false;
163-
}
164-
long x = bombs[i][0] - bombs[j][0];
165-
long y = bombs[i][1] - bombs[j][1];
166-
long r = bombs[i][2];
167-
return r * r >= x * x + y * y;
168-
}
169172
}
170173
```
171174

@@ -176,64 +179,67 @@ class Solution {
176179
public:
177180
int maximumDetonation(vector<vector<int>>& bombs) {
178181
int n = bombs.size();
179-
vector<vector<bool>> g(n, vector<bool>(n));
180-
for (int i = 0; i < n; ++i)
181-
for (int j = 0; j < n; ++j)
182-
g[i][j] = check(i, j, bombs);
182+
vector<int> g[n];
183+
for (int i = 0; i < n - 1; ++i) {
184+
for (int j = i + 1; j < n; ++j) {
185+
auto& p1 = bombs[i];
186+
auto& p2 = bombs[j];
187+
auto dist = hypot(p1[0] - p2[0], p1[1] - p2[1]);
188+
if (dist <= p1[2]) {
189+
g[i].push_back(j);
190+
}
191+
if (dist <= p2[2]) {
192+
g[j].push_back(i);
193+
}
194+
}
195+
}
183196
int ans = 0;
197+
bool vis[n];
184198
for (int k = 0; k < n; ++k) {
185-
queue<int> q{{k}};
186-
vector<bool> vis(n);
199+
memset(vis, false, sizeof(vis));
200+
queue<int> q;
201+
q.push(k);
187202
vis[k] = true;
188203
int cnt = 0;
189204
while (!q.empty()) {
190205
int i = q.front();
191206
q.pop();
192207
++cnt;
193-
for (int j = 0; j < n; ++j) {
194-
if (g[i][j] && !vis[j]) {
208+
for (int j : g[i]) {
209+
if (!vis[j]) {
195210
vis[j] = true;
196211
q.push(j);
197212
}
198213
}
199214
}
215+
if (cnt == n) {
216+
return n;
217+
}
200218
ans = max(ans, cnt);
201219
}
202220
return ans;
203221
}
204-
205-
bool check(int i, int j, vector<vector<int>>& bombs) {
206-
if (i == j) return false;
207-
long long x = bombs[i][0] - bombs[j][0];
208-
long long y = bombs[i][1] - bombs[j][1];
209-
long long r = bombs[i][2];
210-
return r * r >= x * x + y * y;
211-
}
212222
};
213223
```
214224
215225
#### Go
216226
217227
```go
218-
func maximumDetonation(bombs [][]int) int {
219-
check := func(i, j int) bool {
220-
if i == j {
221-
return false
222-
}
223-
x, y := bombs[i][0]-bombs[j][0], bombs[i][1]-bombs[j][1]
224-
r := bombs[i][2]
225-
return r*r >= x*x+y*y
226-
}
228+
func maximumDetonation(bombs [][]int) (ans int) {
227229
n := len(bombs)
228-
g := make([][]bool, n)
229-
for i := range g {
230-
g[i] = make([]bool, n)
231-
for j := range g[i] {
232-
g[i][j] = check(i, j)
230+
g := make([][]int, n)
231+
for i, p1 := range bombs[:n-1] {
232+
for j := i + 1; j < n; j++ {
233+
p2 := bombs[j]
234+
dist := math.Hypot(float64(p1[0]-p2[0]), float64(p1[1]-p2[1]))
235+
if dist <= float64(p1[2]) {
236+
g[i] = append(g[i], j)
237+
}
238+
if dist <= float64(p2[2]) {
239+
g[j] = append(g[j], i)
240+
}
233241
}
234242
}
235-
236-
ans := 0
237243
for k := 0; k < n; k++ {
238244
q := []int{k}
239245
vis := make([]bool, n)
@@ -243,16 +249,19 @@ func maximumDetonation(bombs [][]int) int {
243249
i := q[0]
244250
q = q[1:]
245251
cnt++
246-
for j := 0; j < n; j++ {
247-
if g[i][j] && !vis[j] {
252+
for _, j := range g[i] {
253+
if !vis[j] {
248254
vis[j] = true
249255
q = append(q, j)
250256
}
251257
}
252258
}
259+
if cnt == n {
260+
return n
261+
}
253262
ans = max(ans, cnt)
254263
}
255-
return ans
264+
return
256265
}
257266
```
258267

@@ -261,37 +270,38 @@ func maximumDetonation(bombs [][]int) int {
261270
```ts
262271
function maximumDetonation(bombs: number[][]): number {
263272
const n = bombs.length;
264-
const g = new Map<number, number[]>(bombs.map((_, i) => [i, []]));
265-
266-
for (let i = 0; i < n - 1; i++) {
267-
for (let j = 1; j < n; j++) {
268-
const [x1, y1, r1] = bombs[i];
273+
const g: number[][] = Array.from({ length: n }, () => []);
274+
for (let i = 0; i < n - 1; ++i) {
275+
const [x1, y1, r1] = bombs[i];
276+
for (let j = i + 1; j < n; ++j) {
269277
const [x2, y2, r2] = bombs[j];
270-
const distance = Math.hypot(x1 - x2, y1 - y2);
271-
272-
if (distance <= r1) g.get(i)!.push(j);
273-
if (distance <= r2) g.get(j)!.push(i);
278+
const d = Math.hypot(x1 - x2, y1 - y2);
279+
if (d <= r1) {
280+
g[i].push(j);
281+
}
282+
if (d <= r2) {
283+
g[j].push(i);
284+
}
274285
}
275286
}
276-
277-
let res = 0;
278-
for (let i = 0; i < n; i++) {
279-
const seen = new Set<number>([i]);
280-
const q = [i];
281-
287+
let ans = 0;
288+
for (let k = 0; k < n; ++k) {
289+
const vis: Set<number> = new Set([k]);
290+
const q: number[] = [k];
282291
for (const i of q) {
283-
for (const j of g.get(i) ?? []) {
284-
if (seen.has(j)) continue;
285-
seen.add(j);
286-
q.push(j);
292+
for (const j of g[i]) {
293+
if (!vis.has(j)) {
294+
vis.add(j);
295+
q.push(j);
296+
}
287297
}
288298
}
289-
290-
if (seen.size === n) return n;
291-
res = Math.max(res, seen.size);
299+
if (vis.size === n) {
300+
return n;
301+
}
302+
ans = Math.max(ans, vis.size);
292303
}
293-
294-
return res;
304+
return ans;
295305
}
296306
```
297307

‎solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,44 @@ class Solution {
22
public:
33
int maximumDetonation(vector<vector<int>>& bombs) {
44
int n = bombs.size();
5-
vector<vector<bool>> g(n, vector<bool>(n));
6-
for (int i = 0; i < n; ++i)
7-
for (int j = 0; j < n; ++j)
8-
g[i][j] = check(i, j, bombs);
5+
vector<int> g[n];
6+
for (int i = 0; i < n - 1; ++i) {
7+
for (int j = i + 1; j < n; ++j) {
8+
auto& p1 = bombs[i];
9+
auto& p2 = bombs[j];
10+
auto dist = hypot(p1[0] - p2[0], p1[1] - p2[1]);
11+
if (dist <= p1[2]) {
12+
g[i].push_back(j);
13+
}
14+
if (dist <= p2[2]) {
15+
g[j].push_back(i);
16+
}
17+
}
18+
}
919
int ans = 0;
20+
bool vis[n];
1021
for (int k = 0; k < n; ++k) {
11-
queue<int> q{{k}};
12-
vector<bool> vis(n);
22+
memset(vis, false, sizeof(vis));
23+
queue<int> q;
24+
q.push(k);
1325
vis[k] = true;
1426
int cnt = 0;
1527
while (!q.empty()) {
1628
int i = q.front();
1729
q.pop();
1830
++cnt;
19-
for (int j = 0; j < n; ++j) {
20-
if (g[i][j] && !vis[j]) {
31+
for (int j : g[i]) {
32+
if (!vis[j]) {
2133
vis[j] = true;
2234
q.push(j);
2335
}
2436
}
2537
}
38+
if (cnt == n) {
39+
return n;
40+
}
2641
ans = max(ans, cnt);
2742
}
2843
return ans;
2944
}
30-
31-
bool check(int i, int j, vector<vector<int>>& bombs) {
32-
if (i == j) return false;
33-
long long x = bombs[i][0] - bombs[j][0];
34-
long long y = bombs[i][1] - bombs[j][1];
35-
long long r = bombs[i][2];
36-
return r * r >= x * x + y * y;
37-
}
3845
};
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
func maximumDetonation(bombs [][]int) int {
2-
check := func(i, j int) bool {
3-
if i == j {
4-
return false
5-
}
6-
x, y := bombs[i][0]-bombs[j][0], bombs[i][1]-bombs[j][1]
7-
r := bombs[i][2]
8-
return r*r >= x*x+y*y
9-
}
1+
func maximumDetonation(bombs [][]int) (ans int) {
102
n := len(bombs)
11-
g := make([][]bool, n)
12-
for i := range g {
13-
g[i] = make([]bool, n)
14-
for j := range g[i] {
15-
g[i][j] = check(i, j)
3+
g := make([][]int, n)
4+
for i, p1 := range bombs[:n-1] {
5+
for j := i + 1; j < n; j++ {
6+
p2 := bombs[j]
7+
dist := math.Hypot(float64(p1[0]-p2[0]), float64(p1[1]-p2[1]))
8+
if dist <= float64(p1[2]) {
9+
g[i] = append(g[i], j)
10+
}
11+
if dist <= float64(p2[2]) {
12+
g[j] = append(g[j], i)
13+
}
1614
}
1715
}
18-
19-
ans := 0
2016
for k := 0; k < n; k++ {
2117
q := []int{k}
2218
vis := make([]bool, n)
@@ -26,14 +22,17 @@ func maximumDetonation(bombs [][]int) int {
2622
i := q[0]
2723
q = q[1:]
2824
cnt++
29-
for j := 0; j < n; j++ {
30-
if g[i][j] && !vis[j] {
25+
for _, j := range g[i] {
26+
if !vis[j] {
3127
vis[j] = true
3228
q = append(q, j)
3329
}
3430
}
3531
}
32+
if cnt == n {
33+
return n
34+
}
3635
ans = max(ans, cnt)
3736
}
38-
return ans
37+
return
3938
}
Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
11
class Solution {
2-
private int[][] bombs;
3-
42
public int maximumDetonation(int[][] bombs) {
5-
this.bombs = bombs;
63
int n = bombs.length;
7-
boolean[][] g = new boolean[n][n];
8-
for (int i = 0; i < n; ++i) {
9-
for (int j = 0; j < n; ++j) {
10-
g[i][j] = check(i, j);
4+
List<Integer>[] g = new List[n];
5+
Arrays.setAll(g, k -> new ArrayList<>());
6+
for (int i = 0; i < n - 1; ++i) {
7+
for (int j = i + 1; j < n; ++j) {
8+
int[] p1 = bombs[i], p2 = bombs[j];
9+
double dist = Math.hypot(p1[0] - p2[0], p1[1] - p2[1]);
10+
if (dist <= p1[2]) {
11+
g[i].add(j);
12+
}
13+
if (dist <= p2[2]) {
14+
g[j].add(i);
15+
}
1116
}
1217
}
1318
int ans = 0;
19+
boolean[] vis = new boolean[n];
1420
for (int k = 0; k < n; ++k) {
15-
Deque<Integer> q = new ArrayDeque<>();
16-
q.offer(k);
17-
boolean[] vis = new boolean[n];
21+
Arrays.fill(vis, false);
1822
vis[k] = true;
1923
int cnt = 0;
24+
Deque<Integer> q = new ArrayDeque<>();
25+
q.offer(k);
2026
while (!q.isEmpty()) {
2127
int i = q.poll();
2228
++cnt;
23-
for (int j = 0; j < n; ++j) {
24-
if (g[i][j] && !vis[j]) {
29+
for (int j : g[i]) {
30+
if (!vis[j]) {
2531
vis[j] = true;
2632
q.offer(j);
2733
}
2834
}
2935
}
36+
if (cnt == n) {
37+
return n;
38+
}
3039
ans = Math.max(ans, cnt);
3140
}
3241
return ans;
3342
}
34-
35-
private boolean check(int i, int j) {
36-
if (i == j) {
37-
return false;
38-
}
39-
long x = bombs[i][0] - bombs[j][0];
40-
long y = bombs[i][1] - bombs[j][1];
41-
long r = bombs[i][2];
42-
return r * r >= x * x + y * y;
43-
}
4443
}
Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
class Solution:
22
def maximumDetonation(self, bombs: List[List[int]]) -> int:
3-
def check(i, j):
4-
if i == j:
5-
return False
6-
x, y = bombs[i][0] - bombs[j][0], bombs[i][1] - bombs[j][1]
7-
r = bombs[i][2]
8-
return r * r >= x * x + y * y
9-
10-
g = defaultdict(list)
113
n = len(bombs)
12-
for i in range(n):
13-
for j in range(n):
14-
if check(i, j):
4+
g = [[] for _ in range(n)]
5+
for i in range(n - 1):
6+
x1, y1, r1 = bombs[i]
7+
for j in range(i + 1, n):
8+
x2, y2, r2 = bombs[j]
9+
dist = hypot(x1 - x2, y1 - y2)
10+
if dist <= r1:
1511
g[i].append(j)
12+
if dist <= r2:
13+
g[j].append(i)
1614
ans = 0
1715
for k in range(n):
18-
q = deque([k])
19-
vis = [False] * n
20-
vis[k] = True
21-
cnt = 0
22-
while q:
23-
i = q.popleft()
24-
cnt += 1
16+
vis = {k}
17+
q = [k]
18+
for i in q:
2519
for j in g[i]:
26-
if not vis[j]:
27-
vis[j] = True
20+
if j not in vis:
21+
vis.add(j)
2822
q.append(j)
29-
ans = max(ans, cnt)
23+
if len(vis) == n:
24+
return n
25+
ans = max(ans, len(vis))
3026
return ans
Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
function maximumDetonation(bombs: number[][]): number {
22
const n = bombs.length;
3-
const g = new Map<number, number[]>(bombs.map((_, i) => [i, []]));
4-
5-
for (let i = 0; i < n - 1; i++) {
6-
for (let j = 1; j < n; j++) {
7-
const [x1, y1, r1] = bombs[i];
3+
const g: number[][] = Array.from({ length: n }, () => []);
4+
for (let i = 0; i < n - 1; ++i) {
5+
const [x1, y1, r1] = bombs[i];
6+
for (let j = i + 1; j < n; ++j) {
87
const [x2, y2, r2] = bombs[j];
9-
const distance = Math.hypot(x1 - x2, y1 - y2);
10-
11-
if (distance <= r1) g.get(i)!.push(j);
12-
if (distance <= r2) g.get(j)!.push(i);
8+
const d = Math.hypot(x1 - x2, y1 - y2);
9+
if (d <= r1) {
10+
g[i].push(j);
11+
}
12+
if (d <= r2) {
13+
g[j].push(i);
14+
}
1315
}
1416
}
15-
16-
let res = 0;
17-
for (let i = 0; i < n; i++) {
18-
const seen = new Set<number>([i]);
19-
const q = [i];
20-
17+
let ans = 0;
18+
for (let k = 0; k < n; ++k) {
19+
const vis: Set<number> = new Set([k]);
20+
const q: number[] = [k];
2121
for (const i of q) {
22-
for (const j of g.get(i) ?? []) {
23-
if (seen.has(j)) continue;
24-
seen.add(j);
25-
q.push(j);
22+
for (const j of g[i]) {
23+
if (!vis.has(j)) {
24+
vis.add(j);
25+
q.push(j);
26+
}
2627
}
2728
}
28-
29-
if (seen.size === n) return n;
30-
res = Math.max(res, seen.size);
29+
if (vis.size === n) {
30+
return n;
31+
}
32+
ans = Math.max(ans, vis.size);
3133
}
32-
33-
return res;
34+
return ans;
3435
}

0 commit comments

Comments
 (0)
Please sign in to comment.