目标
现有 n 个机器人,编号从 1 开始,每个机器人包含在路线上的位置、健康度和移动方向。
给你下标从 0 开始的两个整数数组 positions、healths 和一个字符串 directions(directions[i] 为 'L' 表示 向左 或 'R' 表示 向右)。 positions 中的所有整数 互不相同 。
所有机器人以 相同速度 同时 沿给定方向在路线上移动。如果两个机器人移动到相同位置,则会发生 碰撞 。
如果两个机器人发生碰撞,则将 健康度较低 的机器人从路线中 移除 ,并且另一个机器人的健康度 减少 1 。幸存下来的机器人将会继续沿着与之前 相同 的方向前进。如果两个机器人的健康度相同,则将二者都从路线中移除。
请你确定全部碰撞后幸存下的所有机器人的 健康度 ,并按照原来机器人编号的顺序排列。即机器人 1 (如果幸存)的最终健康度,机器人 2 (如果幸存)的最终健康度等。 如果不存在幸存的机器人,则返回空数组。
在不再发生任何碰撞后,请你以数组形式,返回所有剩余机器人的健康度(按机器人输入中的编号顺序)。
注意:位置 positions 可能是乱序的。
示例 1:

输入:positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = "RRRRR"
输出:[2,17,9,15,10]
解释:在本例中不存在碰撞,因为所有机器人向同一方向移动。所以,从第一个机器人开始依序返回健康度,[2, 17, 9, 15, 10] 。
示例 2:

输入:positions = [3,5,2,6], healths = [10,10,15,12], directions = "RLRL"
输出:[14]
解释:本例中发生 2 次碰撞。首先,机器人 1 和机器人 2 将会碰撞,因为二者健康度相同,二者都将被从路线中移除。接下来,机器人 3 和机器人 4 将会发生碰撞,由于机器人 4 的健康度更小,则它会被移除,而机器人 3 的健康度变为 15 - 1 = 14 。仅剩机器人 3 ,所以返回 [14] 。
示例 3:

输入:positions = [1,2,5,6], healths = [10,10,11,11], directions = "RLRL"
输出:[]
解释:机器人 1 和机器人 2 将会碰撞,因为二者健康度相同,二者都将被从路线中移除。机器人 3 和机器人 4 将会碰撞,因为二者健康度相同,二者都将被从路线中移除。所以返回空数组 [] 。
说明:
- 1 <= positions.length == healths.length == directions.length == n <= 10^5
- 1 <= positions[i], healths[i] <= 10^9
- directions[i] == 'L' 或 directions[i] == 'R'
- positions 中的所有值互不相同
思路
水平线上有 n 个机器人,第 i 个机器人的位置在 positions[i],移动方向为 directions[i],健康度为 healths[i],每个机器人都以 相同的速度 沿着 directions[i] 方向移动,如果发生碰撞,健康度大的减一,小的归零,如果相等则一起归零,当健康度归零时,从水平线上移除。求剩余机器人的健康度,按照原来机器人的编号顺序输出。
类似于括号匹配,只不过是有条件的匹配。如果健康度相同可以抵消,否则健康度低的被移除,健康度高的减一。模拟碰撞过程即可。
代码
/**
* @date 2026-04-01 9:01
*/
public class SurvivedRobotsHealths2751 {
public List<Integer> survivedRobotsHealths(int[] positions, int[] healths, String directions) {
int n = positions.length;
int[][] robots = new int[n][4];
for (int i = 0; i < n; i++) {
// 编号、位置、健康度、移动方向 1 向右,-1 向左
robots[i] = new int[]{i + 1, positions[i], healths[i], directions.charAt(i) == 'R' ? 1 : -1};
}
Arrays.sort(robots, (a, b) -> a[1] - b[1]);
Deque<int[]> q = new ArrayDeque<>();
for (int[] robot : robots) {
if (robot[3] == 1) {
q.push(robot);
} else {
if (q.isEmpty()) {
q.push(robot);
} else {
while (!q.isEmpty() && q.peek()[3] == 1) {
if (q.peek()[2] < robot[2]) {
q.poll();
robot[2]--;
} else if (q.peek()[2] == robot[2]) {
q.poll();
robot[2] = 0;
break;
} else {
q.peek()[2]--;
robot[2] = 0;
break;
}
}
if (robot[2] > 0) {
q.push(robot);
}
}
}
}
List<int[]> list = new ArrayList<>(q);
list.sort((a, b) -> a[0] - b[0]);
return list.stream().mapToInt(x -> x[2]).boxed().collect(Collectors.toList());
}
}
性能
