Skip to content

Commit 2825443

Browse files
committed
Fixed sonar
1 parent 67df942 commit 2825443

File tree

3 files changed

+167
-66
lines changed

3 files changed

+167
-66
lines changed

src/main/java/g3601_3700/s3661_maximum_walls_destroyed_by_robots/Solution.java

Lines changed: 104 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,89 +3,127 @@
33
// #Hard #Weekly_Contest_464 #2025_08_29_Time_88_ms_(99.25%)_Space_68.62_MB_(40.75%)
44

55
import java.util.Arrays;
6+
import java.util.Comparator;
67

8+
@SuppressWarnings("java:S6541")
79
public class Solution {
810
public int maxWalls(int[] robots, int[] distance, int[] walls) {
911
if (robots.length == 1) {
10-
int a = 0;
11-
int b = 0;
12-
for (int wall : walls) {
13-
if (wall < robots[0] - distance[0] || wall > robots[0] + distance[0]) {
14-
continue;
15-
}
16-
if (wall < robots[0]) {
17-
a++;
18-
} else if (wall > robots[0]) {
19-
b++;
20-
} else {
21-
a++;
22-
b++;
23-
}
12+
return handleSingleRobot(robots[0], distance[0], walls);
13+
}
14+
int[][] arr = buildRobotArray(robots, distance);
15+
Arrays.sort(arr, Comparator.comparingInt(a -> a[0]));
16+
Arrays.sort(walls);
17+
return processMultipleRobots(arr, walls);
18+
}
19+
20+
private int handleSingleRobot(int robot, int dist, int[] walls) {
21+
int left = 0, right = 0;
22+
for (int wall : walls) {
23+
if (wall < robot - dist || wall > robot + dist) {
24+
continue;
25+
}
26+
if (wall < robot) {
27+
left++;
28+
} else if (wall > robot) {
29+
right++;
30+
} else {
31+
left++;
32+
right++;
2433
}
25-
return Math.max(a, b);
2634
}
27-
int[][] arr = new int[robots.length][];
35+
return Math.max(left, right);
36+
}
37+
38+
private int[][] buildRobotArray(int[] robots, int[] distance) {
39+
int[][] arr = new int[robots.length][2];
2840
for (int i = 0; i < robots.length; i++) {
29-
arr[i] = new int[] {robots[i], distance[i]};
41+
arr[i][0] = robots[i];
42+
arr[i][1] = distance[i];
3043
}
31-
Arrays.sort(arr, (a, b) -> a[0] - b[0]);
32-
Arrays.sort(walls);
33-
int a = 0;
34-
int b = 0;
44+
return arr;
45+
}
46+
47+
private int processMultipleRobots(int[][] arr, int[] walls) {
48+
int a;
49+
int b;
3550
int i = 0;
3651
int j = 0;
37-
while (i < walls.length && walls[i] < arr[j][0] - arr[j][1]) {
38-
i++;
39-
}
40-
while (i < walls.length && walls[i] <= arr[j][0]) {
41-
a++;
42-
i++;
43-
}
52+
i = skipWallsBeforeRange(walls, i, arr[j][0] - arr[j][1]);
53+
a = countWallsUpToRobot(walls, i, arr[j][0]);
54+
i += a;
4455
if (i > 0 && walls[i - 1] == arr[j][0]) {
4556
i--;
4657
}
47-
while (i < walls.length && walls[i] <= arr[j][0] + arr[j][1] && walls[i] < arr[j + 1][0]) {
48-
b++;
49-
i++;
50-
}
58+
b = countWallsInRange(walls, i, arr[j][0], arr[j][0] + arr[j][1], arr[j + 1][0]);
59+
i += b;
5160
j++;
5261
while (j < arr.length) {
53-
int l1 = 0;
54-
int k = i;
55-
while (k < walls.length && walls[k] < arr[j][0] - arr[j][1]) {
56-
k++;
57-
}
58-
while (k < walls.length && walls[k] <= arr[j][0]) {
59-
l1++;
60-
k++;
61-
}
62-
int nextI = k;
63-
int l2 = l1;
64-
k = i - 1;
65-
while (k >= 0 && walls[k] > arr[j - 1][0] && walls[k] >= arr[j][0] - arr[j][1]) {
66-
l2++;
67-
k--;
68-
}
69-
int aNext = Math.max(a + l2, b + l1);
70-
int r = 0;
71-
int lim =
72-
j < arr.length - 1
73-
? Math.min(arr[j + 1][0], arr[j][0] + arr[j][1] + 1)
74-
: arr[j][0] + arr[j][1] + 1;
75-
if (nextI > 0 && walls[nextI - 1] == arr[j][0]) {
76-
i = nextI - 1;
77-
} else {
78-
i = nextI;
79-
}
80-
while (i < walls.length && walls[i] < lim) {
81-
r++;
82-
i++;
83-
}
62+
int[] result = processRobotStep(arr, walls, j, i, a, b);
63+
a = result[0];
64+
b = result[1];
65+
i = result[2];
8466
j++;
85-
int bNext = Math.max(a, b) + r;
86-
a = aNext;
87-
b = bNext;
8867
}
8968
return Math.max(a, b);
9069
}
70+
71+
private int skipWallsBeforeRange(int[] walls, int i, int limit) {
72+
while (i < walls.length && walls[i] < limit) {
73+
i++;
74+
}
75+
return i;
76+
}
77+
78+
private int countWallsUpToRobot(int[] walls, int i, int robotPos) {
79+
int count = 0;
80+
while (i + count < walls.length && walls[i + count] <= robotPos) {
81+
count++;
82+
}
83+
return count;
84+
}
85+
86+
private int countWallsInRange(int[] walls, int i, int robotPos, int maxReach, int nextRobot) {
87+
int count = 0;
88+
while (i + count < walls.length
89+
&& walls[i + count] <= maxReach
90+
&& walls[i + count] < nextRobot) {
91+
count++;
92+
}
93+
return count;
94+
}
95+
96+
private int[] processRobotStep(int[][] arr, int[] walls, int j, int i, int a, int b) {
97+
int l1 = 0;
98+
int k = i;
99+
while (k < walls.length && walls[k] < arr[j][0] - arr[j][1]) k++;
100+
while (k < walls.length && walls[k] <= arr[j][0]) {
101+
l1++;
102+
k++;
103+
}
104+
int nextI = k;
105+
int l2 = l1;
106+
k = i - 1;
107+
while (k >= 0 && walls[k] > arr[j - 1][0] && walls[k] >= arr[j][0] - arr[j][1]) {
108+
l2++;
109+
k--;
110+
}
111+
int aNext = Math.max(a + l2, b + l1);
112+
int r = 0;
113+
int lim =
114+
(j < arr.length - 1)
115+
? Math.min(arr[j + 1][0], arr[j][0] + arr[j][1] + 1)
116+
: arr[j][0] + arr[j][1] + 1;
117+
if (nextI > 0 && walls[nextI - 1] == arr[j][0]) {
118+
i = nextI - 1;
119+
} else {
120+
i = nextI;
121+
}
122+
while (i < walls.length && walls[i] < lim) {
123+
r++;
124+
i++;
125+
}
126+
int bNext = Math.max(a, b) + r;
127+
return new int[] {aNext, bNext, i};
128+
}
91129
}

src/test/java/g3601_3700/s3658_gcd_of_odd_and_even_sums/SolutionTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,19 @@ void gcdOfOddEvenSums() {
1515
void gcdOfOddEvenSums2() {
1616
assertThat(new Solution().gcdOfOddEvenSums(5), equalTo(5));
1717
}
18+
19+
@Test
20+
void gcdOfOddEvenSums3() {
21+
assertThat(new Solution().gcdOfOddEvenSums(42), equalTo(42));
22+
}
23+
24+
@Test
25+
void gcdOfOddEvenSums4() {
26+
assertThat(new Solution().gcdOfOddEvenSums(-42), equalTo(42));
27+
}
28+
29+
@Test
30+
void gcdOfOddEvenSums5() {
31+
assertThat(new Solution().gcdOfOddEvenSums(0), equalTo(0));
32+
}
1833
}

src/test/java/g3601_3700/s3661_maximum_walls_destroyed_by_robots/SolutionTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,52 @@ void maxWalls3() {
2626
new Solution().maxWalls(new int[] {1, 2}, new int[] {100, 1}, new int[] {10}),
2727
equalTo(0));
2828
}
29+
30+
@Test
31+
void maxWalls4() {
32+
int[] robots = {5};
33+
int[] distance = {3};
34+
int[] walls = {};
35+
assertThat(new Solution().maxWalls(robots, distance, walls), equalTo(0));
36+
}
37+
38+
@Test
39+
void maxWalls5() {
40+
int[] robots = {5};
41+
int[] distance = {3};
42+
int[] walls = {2, 4, 5, 6, 8};
43+
assertThat(new Solution().maxWalls(robots, distance, walls), equalTo(3));
44+
}
45+
46+
@Test
47+
void maxWalls6() {
48+
int[] robots = {10};
49+
int[] distance = {2};
50+
int[] walls = {7, 8, 9, 10, 11, 13};
51+
assertThat(new Solution().maxWalls(robots, distance, walls), equalTo(3));
52+
}
53+
54+
@Test
55+
void maxWalls7() {
56+
int[] robots = {5, 15};
57+
int[] distance = {2, 2};
58+
int[] walls = {4, 5, 6, 14, 15, 16};
59+
assertThat(new Solution().maxWalls(robots, distance, walls), equalTo(4));
60+
}
61+
62+
@Test
63+
void maxWalls8() {
64+
int[] robots = {5, 8};
65+
int[] distance = {5, 5};
66+
int[] walls = {2, 4, 5, 6, 7, 8, 9, 10};
67+
assertThat(new Solution().maxWalls(robots, distance, walls), equalTo(6));
68+
}
69+
70+
@Test
71+
void maxWalls9() {
72+
int[] robots = {3, 10, 20};
73+
int[] distance = {2, 3, 4};
74+
int[] walls = {1, 2, 3, 4, 5, 8, 10, 12, 17, 19, 20, 22};
75+
assertThat(new Solution().maxWalls(robots, distance, walls), equalTo(8));
76+
}
2977
}

0 commit comments

Comments
 (0)