diff --git a/.prettierignore b/.prettierignore index 1e8a33d8a7e35..f76480ca0d15f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -15,7 +15,6 @@ node_modules/ /solution/bash_problem_readme_template_en.md /solution/0100-0199/0177.Nth Highest Salary/Solution.sql /solution/0100-0199/0178.Rank Scores/Solution2.sql -/solution/0100-0199/0196.Delete Duplicate Emails/Solution3.sql /solution/0500-0599/0586.Customer Placing the Largest Number of Orders/Solution2.sql /solution/1400-1499/1454.Active Users/Solution.sql /solution/1600-1699/1635.Hopper Company Queries I/Solution.sql diff --git a/.prettierrc b/.prettierrc index d5e29fa69e318..47eab9be9a8a3 100644 --- a/.prettierrc +++ b/.prettierrc @@ -11,6 +11,7 @@ "braceStyle": "1tbs", "endOfLine": "lf", "sqlKeywordCase": "upper", + "sqlCanonicalSyntax": false, "overrides": [ { "files": ["*.sql"], diff --git a/solution/2100-2199/2101.Detonate the Maximum Bombs/README.md b/solution/2100-2199/2101.Detonate the Maximum Bombs/README.md index 868f54020956d..4121be84afe0c 100644 --- a/solution/2100-2199/2101.Detonate the Maximum Bombs/README.md +++ b/solution/2100-2199/2101.Detonate the Maximum Bombs/README.md @@ -88,7 +88,13 @@ tags: ### 方法一:BFS -枚举每个炸弹 k 作为起始引爆点,BFS 搜索能影响到的所有炸弹的数量,取其最大值。 +我们定义一个长度为 $n$ 的数组 $g$,其中 $g[i]$ 表示炸弹 $i$ 的爆炸范围内可以引爆的所有炸弹的下标。 + +然后,我们遍历所有炸弹,对于两个炸弹 $(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]$ 中。 + +接下来,我们遍历所有炸弹,对于每个炸弹 $k$,我们使用广度优先搜索计算炸弹 $k$ 的爆炸范围内可以引爆的所有炸弹的下标,并记录下来。如果这些炸弹的数量等于 $n$,那么我们就可以引爆所有炸弹,直接返回 $n$。否则,我们记录下来这些炸弹的数量,并返回最大值。 + +时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 为炸弹的数量。 @@ -97,33 +103,29 @@ tags: ```python class Solution: def maximumDetonation(self, bombs: List[List[int]]) -> int: - def check(i, j): - if i == j: - return False - x, y = bombs[i][0] - bombs[j][0], bombs[i][1] - bombs[j][1] - r = bombs[i][2] - return r * r >= x * x + y * y - - g = defaultdict(list) n = len(bombs) - for i in range(n): - for j in range(n): - if check(i, j): + g = [[] for _ in range(n)] + for i in range(n - 1): + x1, y1, r1 = bombs[i] + for j in range(i + 1, n): + x2, y2, r2 = bombs[j] + dist = hypot(x1 - x2, y1 - y2) + if dist <= r1: g[i].append(j) + if dist <= r2: + g[j].append(i) ans = 0 for k in range(n): - q = deque([k]) - vis = [False] * n - vis[k] = True - cnt = 0 - while q: - i = q.popleft() - cnt += 1 + vis = {k} + q = [k] + for i in q: for j in g[i]: - if not vis[j]: - vis[j] = True + if j not in vis: + vis.add(j) q.append(j) - ans = max(ans, cnt) + if len(vis) == n: + return n + ans = max(ans, len(vis)) return ans ``` @@ -131,48 +133,47 @@ class Solution: ```java class Solution { - private int[][] bombs; - public int maximumDetonation(int[][] bombs) { - this.bombs = bombs; int n = bombs.length; - boolean[][] g = new boolean[n][n]; - for (int i = 0; i < n; ++i) { - for (int j = 0; j < n; ++j) { - g[i][j] = check(i, j); + List[] g = new List[n]; + Arrays.setAll(g, k -> new ArrayList<>()); + for (int i = 0; i < n - 1; ++i) { + for (int j = i + 1; j < n; ++j) { + int[] p1 = bombs[i], p2 = bombs[j]; + double dist = Math.hypot(p1[0] - p2[0], p1[1] - p2[1]); + if (dist <= p1[2]) { + g[i].add(j); + } + if (dist <= p2[2]) { + g[j].add(i); + } } } int ans = 0; + boolean[] vis = new boolean[n]; for (int k = 0; k < n; ++k) { - Deque q = new ArrayDeque<>(); - q.offer(k); - boolean[] vis = new boolean[n]; + Arrays.fill(vis, false); vis[k] = true; int cnt = 0; + Deque q = new ArrayDeque<>(); + q.offer(k); while (!q.isEmpty()) { int i = q.poll(); ++cnt; - for (int j = 0; j < n; ++j) { - if (g[i][j] && !vis[j]) { + for (int j : g[i]) { + if (!vis[j]) { vis[j] = true; q.offer(j); } } } + if (cnt == n) { + return n; + } ans = Math.max(ans, cnt); } return ans; } - - private boolean check(int i, int j) { - if (i == j) { - return false; - } - long x = bombs[i][0] - bombs[j][0]; - long y = bombs[i][1] - bombs[j][1]; - long r = bombs[i][2]; - return r * r >= x * x + y * y; - } } ``` @@ -183,64 +184,67 @@ class Solution { public: int maximumDetonation(vector>& bombs) { int n = bombs.size(); - vector> g(n, vector(n)); - for (int i = 0; i < n; ++i) - for (int j = 0; j < n; ++j) - g[i][j] = check(i, j, bombs); + vector g[n]; + for (int i = 0; i < n - 1; ++i) { + for (int j = i + 1; j < n; ++j) { + auto& p1 = bombs[i]; + auto& p2 = bombs[j]; + auto dist = hypot(p1[0] - p2[0], p1[1] - p2[1]); + if (dist <= p1[2]) { + g[i].push_back(j); + } + if (dist <= p2[2]) { + g[j].push_back(i); + } + } + } int ans = 0; + bool vis[n]; for (int k = 0; k < n; ++k) { - queue q{{k}}; - vector vis(n); + memset(vis, false, sizeof(vis)); + queue q; + q.push(k); vis[k] = true; int cnt = 0; while (!q.empty()) { int i = q.front(); q.pop(); ++cnt; - for (int j = 0; j < n; ++j) { - if (g[i][j] && !vis[j]) { + for (int j : g[i]) { + if (!vis[j]) { vis[j] = true; q.push(j); } } } + if (cnt == n) { + return n; + } ans = max(ans, cnt); } return ans; } - - bool check(int i, int j, vector>& bombs) { - if (i == j) return false; - long long x = bombs[i][0] - bombs[j][0]; - long long y = bombs[i][1] - bombs[j][1]; - long long r = bombs[i][2]; - return r * r >= x * x + y * y; - } }; ``` #### Go ```go -func maximumDetonation(bombs [][]int) int { - check := func(i, j int) bool { - if i == j { - return false - } - x, y := bombs[i][0]-bombs[j][0], bombs[i][1]-bombs[j][1] - r := bombs[i][2] - return r*r >= x*x+y*y - } +func maximumDetonation(bombs [][]int) (ans int) { n := len(bombs) - g := make([][]bool, n) - for i := range g { - g[i] = make([]bool, n) - for j := range g[i] { - g[i][j] = check(i, j) + g := make([][]int, n) + for i, p1 := range bombs[:n-1] { + for j := i + 1; j < n; j++ { + p2 := bombs[j] + dist := math.Hypot(float64(p1[0]-p2[0]), float64(p1[1]-p2[1])) + if dist <= float64(p1[2]) { + g[i] = append(g[i], j) + } + if dist <= float64(p2[2]) { + g[j] = append(g[j], i) + } } } - - ans := 0 for k := 0; k < n; k++ { q := []int{k} vis := make([]bool, n) @@ -250,16 +254,19 @@ func maximumDetonation(bombs [][]int) int { i := q[0] q = q[1:] cnt++ - for j := 0; j < n; j++ { - if g[i][j] && !vis[j] { + for _, j := range g[i] { + if !vis[j] { vis[j] = true q = append(q, j) } } } + if cnt == n { + return n + } ans = max(ans, cnt) } - return ans + return } ``` @@ -268,37 +275,38 @@ func maximumDetonation(bombs [][]int) int { ```ts function maximumDetonation(bombs: number[][]): number { const n = bombs.length; - const g = new Map(bombs.map((_, i) => [i, []])); - - for (let i = 0; i < n - 1; i++) { - for (let j = 1; j < n; j++) { - const [x1, y1, r1] = bombs[i]; + const g: number[][] = Array.from({ length: n }, () => []); + for (let i = 0; i < n - 1; ++i) { + const [x1, y1, r1] = bombs[i]; + for (let j = i + 1; j < n; ++j) { const [x2, y2, r2] = bombs[j]; - const distance = Math.hypot(x1 - x2, y1 - y2); - - if (distance <= r1) g.get(i)!.push(j); - if (distance <= r2) g.get(j)!.push(i); + const d = Math.hypot(x1 - x2, y1 - y2); + if (d <= r1) { + g[i].push(j); + } + if (d <= r2) { + g[j].push(i); + } } } - - let res = 0; - for (let i = 0; i < n; i++) { - const seen = new Set([i]); - const q = [i]; - + let ans = 0; + for (let k = 0; k < n; ++k) { + const vis: Set = new Set([k]); + const q: number[] = [k]; for (const i of q) { - for (const j of g.get(i) ?? []) { - if (seen.has(j)) continue; - seen.add(j); - q.push(j); + for (const j of g[i]) { + if (!vis.has(j)) { + vis.add(j); + q.push(j); + } } } - - if (seen.size === n) return n; - res = Math.max(res, seen.size); + if (vis.size === n) { + return n; + } + ans = Math.max(ans, vis.size); } - - return res; + return ans; } ``` diff --git a/solution/2100-2199/2101.Detonate the Maximum Bombs/README_EN.md b/solution/2100-2199/2101.Detonate the Maximum Bombs/README_EN.md index aa5a4b80b7f54..56cd25ab4440e 100644 --- a/solution/2100-2199/2101.Detonate the Maximum Bombs/README_EN.md +++ b/solution/2100-2199/2101.Detonate the Maximum Bombs/README_EN.md @@ -81,7 +81,15 @@ Thus all 5 bombs are detonated. -### Solution 1 +### Solution 1: BFS + +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. + +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]$. + +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. + +The time complexity is $O(n^2)$ and the space complexity is $O(n^2)$, where $n$ is the number of bombs. @@ -90,33 +98,29 @@ Thus all 5 bombs are detonated. ```python class Solution: def maximumDetonation(self, bombs: List[List[int]]) -> int: - def check(i, j): - if i == j: - return False - x, y = bombs[i][0] - bombs[j][0], bombs[i][1] - bombs[j][1] - r = bombs[i][2] - return r * r >= x * x + y * y - - g = defaultdict(list) n = len(bombs) - for i in range(n): - for j in range(n): - if check(i, j): + g = [[] for _ in range(n)] + for i in range(n - 1): + x1, y1, r1 = bombs[i] + for j in range(i + 1, n): + x2, y2, r2 = bombs[j] + dist = hypot(x1 - x2, y1 - y2) + if dist <= r1: g[i].append(j) + if dist <= r2: + g[j].append(i) ans = 0 for k in range(n): - q = deque([k]) - vis = [False] * n - vis[k] = True - cnt = 0 - while q: - i = q.popleft() - cnt += 1 + vis = {k} + q = [k] + for i in q: for j in g[i]: - if not vis[j]: - vis[j] = True + if j not in vis: + vis.add(j) q.append(j) - ans = max(ans, cnt) + if len(vis) == n: + return n + ans = max(ans, len(vis)) return ans ``` @@ -124,48 +128,47 @@ class Solution: ```java class Solution { - private int[][] bombs; - public int maximumDetonation(int[][] bombs) { - this.bombs = bombs; int n = bombs.length; - boolean[][] g = new boolean[n][n]; - for (int i = 0; i < n; ++i) { - for (int j = 0; j < n; ++j) { - g[i][j] = check(i, j); + List[] g = new List[n]; + Arrays.setAll(g, k -> new ArrayList<>()); + for (int i = 0; i < n - 1; ++i) { + for (int j = i + 1; j < n; ++j) { + int[] p1 = bombs[i], p2 = bombs[j]; + double dist = Math.hypot(p1[0] - p2[0], p1[1] - p2[1]); + if (dist <= p1[2]) { + g[i].add(j); + } + if (dist <= p2[2]) { + g[j].add(i); + } } } int ans = 0; + boolean[] vis = new boolean[n]; for (int k = 0; k < n; ++k) { - Deque q = new ArrayDeque<>(); - q.offer(k); - boolean[] vis = new boolean[n]; + Arrays.fill(vis, false); vis[k] = true; int cnt = 0; + Deque q = new ArrayDeque<>(); + q.offer(k); while (!q.isEmpty()) { int i = q.poll(); ++cnt; - for (int j = 0; j < n; ++j) { - if (g[i][j] && !vis[j]) { + for (int j : g[i]) { + if (!vis[j]) { vis[j] = true; q.offer(j); } } } + if (cnt == n) { + return n; + } ans = Math.max(ans, cnt); } return ans; } - - private boolean check(int i, int j) { - if (i == j) { - return false; - } - long x = bombs[i][0] - bombs[j][0]; - long y = bombs[i][1] - bombs[j][1]; - long r = bombs[i][2]; - return r * r >= x * x + y * y; - } } ``` @@ -176,64 +179,67 @@ class Solution { public: int maximumDetonation(vector>& bombs) { int n = bombs.size(); - vector> g(n, vector(n)); - for (int i = 0; i < n; ++i) - for (int j = 0; j < n; ++j) - g[i][j] = check(i, j, bombs); + vector g[n]; + for (int i = 0; i < n - 1; ++i) { + for (int j = i + 1; j < n; ++j) { + auto& p1 = bombs[i]; + auto& p2 = bombs[j]; + auto dist = hypot(p1[0] - p2[0], p1[1] - p2[1]); + if (dist <= p1[2]) { + g[i].push_back(j); + } + if (dist <= p2[2]) { + g[j].push_back(i); + } + } + } int ans = 0; + bool vis[n]; for (int k = 0; k < n; ++k) { - queue q{{k}}; - vector vis(n); + memset(vis, false, sizeof(vis)); + queue q; + q.push(k); vis[k] = true; int cnt = 0; while (!q.empty()) { int i = q.front(); q.pop(); ++cnt; - for (int j = 0; j < n; ++j) { - if (g[i][j] && !vis[j]) { + for (int j : g[i]) { + if (!vis[j]) { vis[j] = true; q.push(j); } } } + if (cnt == n) { + return n; + } ans = max(ans, cnt); } return ans; } - - bool check(int i, int j, vector>& bombs) { - if (i == j) return false; - long long x = bombs[i][0] - bombs[j][0]; - long long y = bombs[i][1] - bombs[j][1]; - long long r = bombs[i][2]; - return r * r >= x * x + y * y; - } }; ``` #### Go ```go -func maximumDetonation(bombs [][]int) int { - check := func(i, j int) bool { - if i == j { - return false - } - x, y := bombs[i][0]-bombs[j][0], bombs[i][1]-bombs[j][1] - r := bombs[i][2] - return r*r >= x*x+y*y - } +func maximumDetonation(bombs [][]int) (ans int) { n := len(bombs) - g := make([][]bool, n) - for i := range g { - g[i] = make([]bool, n) - for j := range g[i] { - g[i][j] = check(i, j) + g := make([][]int, n) + for i, p1 := range bombs[:n-1] { + for j := i + 1; j < n; j++ { + p2 := bombs[j] + dist := math.Hypot(float64(p1[0]-p2[0]), float64(p1[1]-p2[1])) + if dist <= float64(p1[2]) { + g[i] = append(g[i], j) + } + if dist <= float64(p2[2]) { + g[j] = append(g[j], i) + } } } - - ans := 0 for k := 0; k < n; k++ { q := []int{k} vis := make([]bool, n) @@ -243,16 +249,19 @@ func maximumDetonation(bombs [][]int) int { i := q[0] q = q[1:] cnt++ - for j := 0; j < n; j++ { - if g[i][j] && !vis[j] { + for _, j := range g[i] { + if !vis[j] { vis[j] = true q = append(q, j) } } } + if cnt == n { + return n + } ans = max(ans, cnt) } - return ans + return } ``` @@ -261,37 +270,38 @@ func maximumDetonation(bombs [][]int) int { ```ts function maximumDetonation(bombs: number[][]): number { const n = bombs.length; - const g = new Map(bombs.map((_, i) => [i, []])); - - for (let i = 0; i < n - 1; i++) { - for (let j = 1; j < n; j++) { - const [x1, y1, r1] = bombs[i]; + const g: number[][] = Array.from({ length: n }, () => []); + for (let i = 0; i < n - 1; ++i) { + const [x1, y1, r1] = bombs[i]; + for (let j = i + 1; j < n; ++j) { const [x2, y2, r2] = bombs[j]; - const distance = Math.hypot(x1 - x2, y1 - y2); - - if (distance <= r1) g.get(i)!.push(j); - if (distance <= r2) g.get(j)!.push(i); + const d = Math.hypot(x1 - x2, y1 - y2); + if (d <= r1) { + g[i].push(j); + } + if (d <= r2) { + g[j].push(i); + } } } - - let res = 0; - for (let i = 0; i < n; i++) { - const seen = new Set([i]); - const q = [i]; - + let ans = 0; + for (let k = 0; k < n; ++k) { + const vis: Set = new Set([k]); + const q: number[] = [k]; for (const i of q) { - for (const j of g.get(i) ?? []) { - if (seen.has(j)) continue; - seen.add(j); - q.push(j); + for (const j of g[i]) { + if (!vis.has(j)) { + vis.add(j); + q.push(j); + } } } - - if (seen.size === n) return n; - res = Math.max(res, seen.size); + if (vis.size === n) { + return n; + } + ans = Math.max(ans, vis.size); } - - return res; + return ans; } ``` diff --git a/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.cpp b/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.cpp index 2d7322f3aba77..b61afff78d355 100644 --- a/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.cpp +++ b/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.cpp @@ -2,37 +2,44 @@ class Solution { public: int maximumDetonation(vector>& bombs) { int n = bombs.size(); - vector> g(n, vector(n)); - for (int i = 0; i < n; ++i) - for (int j = 0; j < n; ++j) - g[i][j] = check(i, j, bombs); + vector g[n]; + for (int i = 0; i < n - 1; ++i) { + for (int j = i + 1; j < n; ++j) { + auto& p1 = bombs[i]; + auto& p2 = bombs[j]; + auto dist = hypot(p1[0] - p2[0], p1[1] - p2[1]); + if (dist <= p1[2]) { + g[i].push_back(j); + } + if (dist <= p2[2]) { + g[j].push_back(i); + } + } + } int ans = 0; + bool vis[n]; for (int k = 0; k < n; ++k) { - queue q{{k}}; - vector vis(n); + memset(vis, false, sizeof(vis)); + queue q; + q.push(k); vis[k] = true; int cnt = 0; while (!q.empty()) { int i = q.front(); q.pop(); ++cnt; - for (int j = 0; j < n; ++j) { - if (g[i][j] && !vis[j]) { + for (int j : g[i]) { + if (!vis[j]) { vis[j] = true; q.push(j); } } } + if (cnt == n) { + return n; + } ans = max(ans, cnt); } return ans; } - - bool check(int i, int j, vector>& bombs) { - if (i == j) return false; - long long x = bombs[i][0] - bombs[j][0]; - long long y = bombs[i][1] - bombs[j][1]; - long long r = bombs[i][2]; - return r * r >= x * x + y * y; - } }; \ No newline at end of file diff --git a/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.go b/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.go index beb0f174c8b6f..a684f44863c40 100644 --- a/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.go +++ b/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.go @@ -1,22 +1,18 @@ -func maximumDetonation(bombs [][]int) int { - check := func(i, j int) bool { - if i == j { - return false - } - x, y := bombs[i][0]-bombs[j][0], bombs[i][1]-bombs[j][1] - r := bombs[i][2] - return r*r >= x*x+y*y - } +func maximumDetonation(bombs [][]int) (ans int) { n := len(bombs) - g := make([][]bool, n) - for i := range g { - g[i] = make([]bool, n) - for j := range g[i] { - g[i][j] = check(i, j) + g := make([][]int, n) + for i, p1 := range bombs[:n-1] { + for j := i + 1; j < n; j++ { + p2 := bombs[j] + dist := math.Hypot(float64(p1[0]-p2[0]), float64(p1[1]-p2[1])) + if dist <= float64(p1[2]) { + g[i] = append(g[i], j) + } + if dist <= float64(p2[2]) { + g[j] = append(g[j], i) + } } } - - ans := 0 for k := 0; k < n; k++ { q := []int{k} vis := make([]bool, n) @@ -26,14 +22,17 @@ func maximumDetonation(bombs [][]int) int { i := q[0] q = q[1:] cnt++ - for j := 0; j < n; j++ { - if g[i][j] && !vis[j] { + for _, j := range g[i] { + if !vis[j] { vis[j] = true q = append(q, j) } } } + if cnt == n { + return n + } ans = max(ans, cnt) } - return ans + return } \ No newline at end of file diff --git a/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.java b/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.java index 161b71f10a0c9..fb4e49e230e2f 100644 --- a/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.java +++ b/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.java @@ -1,44 +1,43 @@ class Solution { - private int[][] bombs; - public int maximumDetonation(int[][] bombs) { - this.bombs = bombs; int n = bombs.length; - boolean[][] g = new boolean[n][n]; - for (int i = 0; i < n; ++i) { - for (int j = 0; j < n; ++j) { - g[i][j] = check(i, j); + List[] g = new List[n]; + Arrays.setAll(g, k -> new ArrayList<>()); + for (int i = 0; i < n - 1; ++i) { + for (int j = i + 1; j < n; ++j) { + int[] p1 = bombs[i], p2 = bombs[j]; + double dist = Math.hypot(p1[0] - p2[0], p1[1] - p2[1]); + if (dist <= p1[2]) { + g[i].add(j); + } + if (dist <= p2[2]) { + g[j].add(i); + } } } int ans = 0; + boolean[] vis = new boolean[n]; for (int k = 0; k < n; ++k) { - Deque q = new ArrayDeque<>(); - q.offer(k); - boolean[] vis = new boolean[n]; + Arrays.fill(vis, false); vis[k] = true; int cnt = 0; + Deque q = new ArrayDeque<>(); + q.offer(k); while (!q.isEmpty()) { int i = q.poll(); ++cnt; - for (int j = 0; j < n; ++j) { - if (g[i][j] && !vis[j]) { + for (int j : g[i]) { + if (!vis[j]) { vis[j] = true; q.offer(j); } } } + if (cnt == n) { + return n; + } ans = Math.max(ans, cnt); } return ans; } - - private boolean check(int i, int j) { - if (i == j) { - return false; - } - long x = bombs[i][0] - bombs[j][0]; - long y = bombs[i][1] - bombs[j][1]; - long r = bombs[i][2]; - return r * r >= x * x + y * y; - } } \ No newline at end of file diff --git a/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.py b/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.py index bd4d3a01a3acd..2613bc3ed6795 100644 --- a/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.py +++ b/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.py @@ -1,30 +1,26 @@ class Solution: def maximumDetonation(self, bombs: List[List[int]]) -> int: - def check(i, j): - if i == j: - return False - x, y = bombs[i][0] - bombs[j][0], bombs[i][1] - bombs[j][1] - r = bombs[i][2] - return r * r >= x * x + y * y - - g = defaultdict(list) n = len(bombs) - for i in range(n): - for j in range(n): - if check(i, j): + g = [[] for _ in range(n)] + for i in range(n - 1): + x1, y1, r1 = bombs[i] + for j in range(i + 1, n): + x2, y2, r2 = bombs[j] + dist = hypot(x1 - x2, y1 - y2) + if dist <= r1: g[i].append(j) + if dist <= r2: + g[j].append(i) ans = 0 for k in range(n): - q = deque([k]) - vis = [False] * n - vis[k] = True - cnt = 0 - while q: - i = q.popleft() - cnt += 1 + vis = {k} + q = [k] + for i in q: for j in g[i]: - if not vis[j]: - vis[j] = True + if j not in vis: + vis.add(j) q.append(j) - ans = max(ans, cnt) + if len(vis) == n: + return n + ans = max(ans, len(vis)) return ans diff --git a/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.ts b/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.ts index 7dfa743d246de..2dc17f4419e79 100644 --- a/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.ts +++ b/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.ts @@ -1,34 +1,35 @@ function maximumDetonation(bombs: number[][]): number { const n = bombs.length; - const g = new Map(bombs.map((_, i) => [i, []])); - - for (let i = 0; i < n - 1; i++) { - for (let j = 1; j < n; j++) { - const [x1, y1, r1] = bombs[i]; + const g: number[][] = Array.from({ length: n }, () => []); + for (let i = 0; i < n - 1; ++i) { + const [x1, y1, r1] = bombs[i]; + for (let j = i + 1; j < n; ++j) { const [x2, y2, r2] = bombs[j]; - const distance = Math.hypot(x1 - x2, y1 - y2); - - if (distance <= r1) g.get(i)!.push(j); - if (distance <= r2) g.get(j)!.push(i); + const d = Math.hypot(x1 - x2, y1 - y2); + if (d <= r1) { + g[i].push(j); + } + if (d <= r2) { + g[j].push(i); + } } } - - let res = 0; - for (let i = 0; i < n; i++) { - const seen = new Set([i]); - const q = [i]; - + let ans = 0; + for (let k = 0; k < n; ++k) { + const vis: Set = new Set([k]); + const q: number[] = [k]; for (const i of q) { - for (const j of g.get(i) ?? []) { - if (seen.has(j)) continue; - seen.add(j); - q.push(j); + for (const j of g[i]) { + if (!vis.has(j)) { + vis.add(j); + q.push(j); + } } } - - if (seen.size === n) return n; - res = Math.max(res, seen.size); + if (vis.size === n) { + return n; + } + ans = Math.max(ans, vis.size); } - - return res; + return ans; }