|
3 | 3 | // #Hard #Weekly_Contest_464 #2025_08_29_Time_88_ms_(99.25%)_Space_68.62_MB_(40.75%)
|
4 | 4 |
|
5 | 5 | import java.util.Arrays;
|
| 6 | +import java.util.Comparator; |
6 | 7 |
|
| 8 | +@SuppressWarnings("java:S6541") |
7 | 9 | public class Solution {
|
8 | 10 | public int maxWalls(int[] robots, int[] distance, int[] walls) {
|
9 | 11 | 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++; |
24 | 33 | }
|
25 |
| - return Math.max(a, b); |
26 | 34 | }
|
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]; |
28 | 40 | 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]; |
30 | 43 | }
|
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; |
35 | 50 | int i = 0;
|
36 | 51 | 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; |
44 | 55 | if (i > 0 && walls[i - 1] == arr[j][0]) {
|
45 | 56 | i--;
|
46 | 57 | }
|
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; |
51 | 60 | j++;
|
52 | 61 | 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]; |
84 | 66 | j++;
|
85 |
| - int bNext = Math.max(a, b) + r; |
86 |
| - a = aNext; |
87 |
| - b = bNext; |
88 | 67 | }
|
89 | 68 | return Math.max(a, b);
|
90 | 69 | }
|
| 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 | + } |
91 | 129 | }
|
0 commit comments