Skip to content

Commit a2b7014

Browse files
committed
Leetcode | Detonate the Maximum Bombs | Java
1 parent ec67507 commit a2b7014

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

leetcode/src/main/java/com/devstromo/medium/p2101/Solution.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,67 @@ private int dfs(int current, List<List<Integer>> graph, boolean[] visited) {
4848

4949
return count;
5050
}
51+
52+
public int maximumDetonationBest(int[][] bombs) {
53+
int n = bombs.length;
54+
if (n <= 1) return n;
55+
if (n == 2) return twoBombs(bombs);
56+
57+
byte[][] links = new byte[n][n + 1];
58+
int[] linksLen = new int[n];
59+
for (int b = 0; b < n; b++)
60+
buildLinks(links, linksLen, bombs, bombs[b], b);
61+
62+
int maxLinks = 0;
63+
for (int i = n - 1; i >= 0; i--) {
64+
if (linksLen[i] > maxLinks) maxLinks = linksLen[i];
65+
links[i][linksLen[i]] = (byte) -1;
66+
}
67+
if (maxLinks == 0 || maxLinks == n - 1) return maxLinks + 1;
68+
69+
int maxExplosions = 0;
70+
for (int i = n - 1; i >= 0; i--) {
71+
maxExplosions = Math.max(maxExplosions, countExplosions(links, new boolean[n], i));
72+
if (maxExplosions == n) break;
73+
}
74+
return maxExplosions;
75+
}
76+
77+
78+
private int countExplosions(byte[][] links, boolean[] used, int b) {
79+
used[b] = true;
80+
int explosions = 1;
81+
for (int b1 : links[b]) {
82+
if (b1 < 0) break;
83+
if (!used[b1]) explosions += countExplosions(links, used, b1);
84+
}
85+
return explosions;
86+
}
87+
88+
89+
private void buildLinks(byte[][] links, int[] linksLen, int[][] bombs, int[] bomb, int b) {
90+
int x = bomb[0];
91+
int y = bomb[1];
92+
long radius = (long) bomb[2] * bomb[2];
93+
for (int b1 = links.length - 1; b1 > b; b1--) {
94+
int[] bomb1 = bombs[b1];
95+
long dist = distance(x, y, bomb1[0], bomb1[1]);
96+
if (dist <= radius) links[b][linksLen[b]++] = (byte) b1;
97+
if (dist <= (long) bomb1[2] * bomb1[2]) links[b1][linksLen[b1]++] = (byte) b;
98+
}
99+
}
100+
101+
102+
private long distance(int x1, int y1, int x2, int y2) {
103+
return (long) (x1 - x2) * (x1 - x2) + (long) (y1 - y2) * (y1 - y2);
104+
}
105+
106+
107+
private int twoBombs(int[][] bombs) {
108+
int[] b0 = bombs[0];
109+
int[] b1 = bombs[1];
110+
long dist = distance(b0[0], b0[1], b1[0], b1[1]);
111+
if (dist <= (long) b0[2] * b0[2] || dist <= (long) b1[2] * b1[2]) return 2;
112+
return 1;
113+
}
51114
}

leetcode/src/test/java/com/devstromo/medium/p2101/SolutionUnitTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,29 @@ void testDetonation() {
3434
));
3535
}
3636

37+
@Test
38+
@DisplayName("Test Detonate the Maximum Bombs Best")
39+
void testDetonationBest() {
40+
assertEquals(2, solution.maximumDetonationBest(
41+
new int[][]{
42+
{2, 1, 3},
43+
{6, 1, 4},
44+
}
45+
));
46+
assertEquals(1, solution.maximumDetonationBest(
47+
new int[][]{
48+
{1, 1, 5},
49+
{10, 10, 5},
50+
}
51+
));
52+
assertEquals(5, solution.maximumDetonationBest(
53+
new int[][]{
54+
{1, 2, 3},
55+
{2, 3, 1},
56+
{3, 4, 2},
57+
{4, 5, 3},
58+
{5, 6, 4}
59+
}
60+
));
61+
}
3762
}

0 commit comments

Comments
 (0)