Skip to content

Commit 7c1baa8

Browse files
authored
feat: add solutions to lc problem: No.2101 (#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

-1
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

+1
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

+111-103
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

0 commit comments

Comments
 (0)