There are n1-indexed robots, each having a position on a line, health, and movement direction.
You are given 0-indexed integer arrays positions, healths, and a string directions (directions[i] is either ’ L’ for left or ’ R’ for right). All integers in positions are unique.
All robots start moving on the linesimultaneously at the same speed in their given directions. If two robots ever share the same position while moving, they will collide.
If two robots collide, the robot with lower health is removed from the line, and the health of the other robot decreasesby one. The surviving robot continues in the same direction it was going. If both robots have the same health, they are both**** removed from the line.
Your task is to determine the health of the robots that survive the collisions, in the same order that the robots were given,**** i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.
Return an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.

Input: positions =[5,4,3,2,1], healths =[2,17,9,15,10], directions ="RRRRR"Output: [2,17,9,15,10]Explanation: No collision occurs inthis example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned,[2,17,9,15,10].

Input: positions =[3,5,2,6], healths =[10,10,15,12], directions ="RLRL"Output: [14]Explanation: There are 2 collisions inthis example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4's health is smaller, it gets removed, and robot 3's health becomes 15-1=14. Only robot 3 remains, so we return[14].

Input: positions =[1,2,5,6], healths =[10,10,11,11], directions ="RLRL"Output: []Explanation: Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array,[].
Robots only collide when a right-moving robot meets a left-moving robot. By sorting robots by position and simulating their movement with a stack, we can efficiently process all collisions.
Sort robots by position, keeping track of their original indices. Use a stack to store right-moving robots. For each left-moving robot, pop from the stack and resolve collisions according to the rules. After all collisions, collect the healths of surviving robots in the original order.
from typing import List
classSolution:
defsurvivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]:
n = len(positions)
robots = sorted([(p, h, i, d) for i, (p, h, d) in enumerate(zip(positions, healths, directions))])
stack = []
alive = [True] * n
health = [h for _, h, _, _ in robots]
for i, (pos, h, idx, dir) in enumerate(robots):
if dir =='R':
stack.append(i)
else:
while stack and alive[i]:
j = stack[-1]
ifnot alive[j]:
stack.pop()
continueif health[j] < health[i]:
alive[j] =False health[i] -=1 stack.pop()
elif health[j] == health[i]:
alive[j] =False alive[i] =False stack.pop()
else:
health[j] -=1 alive[i] =False res = [(robots[i][2], health[i]) for i in range(n) if alive[i]]
res.sort()
return [h for _, h in res]