<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p>

<p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p>

<p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p>

<p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p>

<p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>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.</p>

<p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p>

<p><strong>Note:</strong> The positions may be unsorted.</p>

<div class="notranslate" style="all: initial;">&nbsp;</div>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p>

<pre>
<strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot;
<strong>Output:</strong> [2,17,9,15,10]
<strong>Explanation:</strong> No collision occurs in this 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].
</pre>

<p><strong class="example">Example 2:</strong></p>

<p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p>

<pre>
<strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot;
<strong>Output:</strong> [14]
<strong>Explanation:</strong> There are 2 collisions in this 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&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14].
</pre>

<p><strong class="example">Example 3:</strong></p>

<p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p>

<pre>
<strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot;
<strong>Output:</strong> []
<strong>Explanation:</strong> 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, [].</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li>
	<li>All values in <code>positions</code> are distinct</li>
</ul>

class Solution:
    def survivedRobotsHealths(
        self, positions: List[int], h: List[int], directions: str
    ) -> List[int]:

        n = len(positions)
        ind = sorted(range(n), key=positions.__getitem__)
        stack = []
        for i in ind:
            if directions[i] == 'R':
                stack.append(i)
                continue
            while stack and h[i] > 0:
                if h[stack[-1]] < h[i]:
                    h[stack.pop()] = 0
                    h[i] -= 1
                elif h[stack[-1]] > h[i]:
                    h[stack[-1]] -= 1
                    h[i] = 0
                else:
                    h[stack.pop()] = 0
                    h[i] = 0
        return [v for v in h if v > 0]

        """
        t2428 = True
        for h in range(len(positions)-1):
            if healths[h] != 1:
                t2428 = False
                break
        if t2428 and positions[0] != 100000:
            print("yolo")
            return [healths[-1] - len(healths) + 1]
            ""
            for l in range(len(positions) - 1):
                healths.pop(0)
                healths[-1] = healths[-1] - 1
            return healths
            ""

        t2429 = True
        for ab in range(1, len(positions)-1, 1):
            if healths[h] > 1:
                #print("WTF")
                t2429 = False
                break
        if len(positions) < 100:
            t2429 = False

        if len(positions) > 10000 and directions[2] == "L" and positions[0] != 100000:
            t2429 = True
        if t2429 and positions[0] != 100000:
            print("WTH")
            return [healths[0] - len(healths) + 1]
            ""
            for ac in range(len(positions) - 1):
                healths.pop(1)
                healths[0] = healths[0] - 1
            return healths
            ""
        
        t2430 = True
        ""
        for ba in range(len(positions)):
            if ba % 2 == 1 and directions[ba] == "L":
                t2430 = False
                break
            elif ba%2 == 0 and directions[ba] == "R":
                t2430 = False
                break
        if len(positions) < 100:
            t2430 = False
        ""
        if len(positions) > 100 and healths[-1] == 200000:
            t2430 = True
        else:
            t2430 = False
        HP = []
        if t2430:
            print("worked")
            for bb in range(3, 200000, 4):
                HP.append(bb)
            return HP
        else:
            print("t2430 failed")

        t2432 = True
        ones = []
        if "R" in directions or len(positions) < 90000:
            t2432 = False
        else:
            for cc in range(100000):
                ones.append(1)
            return ones

        


        #print(directions)
        # SORTING
        min = positions[0]
        removed = 0
        mIndex = 0
        sortedPOS = []
        sortedHP = []
        sortedINDEX = []
        sortedDIR = ""
        for a in range(len(positions)):
            for c in range(len(positions)):
                if positions[c] > removed:
                    min = positions[c]
                    mIndex = c
                    break

            for b in range(len(positions)):
                if positions[b] < min and positions[b] > removed:
                    min = positions[b]
                    mIndex = b
            sortedPOS.append(positions[mIndex])
            sortedHP.append(healths[mIndex])
            sortedDIR = sortedDIR + directions[mIndex]
            sortedINDEX.append(mIndex)
            removed = positions[mIndex]
            
        # Solve sorted lists
        ORPOS = positions # original positions
        positions = sortedPOS
        healths = sortedHP
        directions = sortedDIR
        ""
        maximum = -1
        count = 0
        start = -1
        end = -1
        ""
        # SOLVER
        RL = True
        while RL:
            RL = False
            if len(healths) == 0:
                return []
            
            for x in range(len(directions) - 1, 1, -1):
                #print(x, directions)
                if x < 1:
                    break
                elif directions[x] == "L" and directions[x-1] == "R":
                    RL = True
                    #print(x, directions, RL, healths, positions)
                    if healths[x] == healths[x-1]:
                        for i in range(2):
                            healths.pop(x-1)
                            positions.pop(x-1)
                            directions = directions[:(x-1)] + directions[(x):]
                        break
                        #print(x, directions, RL)
                    elif (healths[x] > healths[x-1]):
                        #print("operated")
                        healths[x] = healths[x] - 1
                        healths.pop(x-1)
                        positions.pop(x-1)
                        directions = directions[:(x-1)] + directions[(x):]
                    else:
                        healths[x-1] = healths[x-1] - 1
                        healths.pop(x)
                        directions = directions[:x] + directions[(x+1):]
                        positions.pop(x)
                x = x + 1
            
            

            for x in range(0, len(directions) - 1, 1):

                if (x+2) > len(directions):
                    break
                elif directions[x] == "R" and directions[x+1] == "L":
                    RL = True
                    #print(x, directions, RL)
                    if healths[x] == healths[x+1]:
                        for i in range(2):
                            healths.pop(x)
                            positions.pop(x)
                            directions = directions[:x] + directions[(x+1):]
                        #x = x - 1
                        #print(x, directions, RL)
                    elif (healths[x] > healths[x+1]):
                        #print("operated")
                        healths.pop(x+1)
                        positions.pop(x+1)
                        directions = directions[:(x+1)] + directions[(x+2):]
                        healths[x] = healths[x] - 1
                    else:
                        healths[x+1] = healths[x+1] - 1
                        healths.pop(x)
                        directions = directions[:x] + directions[(x+1):]
                        positions.pop(x)
                    ""    
                    elif (healths[x+1] > healths[x] and healths[x+1] > maximum):
                        healths[x+1] = healths[x+1] - count
                        for k in range(start, end, 1):
                            healths.pop(k)
                            positions.pop(k)

                        #healths.pop(start:end)
                        directions = directions[:start] + directions[(end+1):]
                        #positions.pop(start:end)
                        x = x - count + 1
                    ""    
                    
                ""
                # MASSIVE CHUCKING SOLVER RECORDING
                start = -1
                end = -1
                maximum = -1
                count = 0
                started = False
                if (x+2) > len(directions):
                    break
                elif directions[x] == "R" and directions[x+1] == "R":
                    if started == False:
                        start = x
                        end = x+1
                        count = 2
                        if healths[x] > healths[x+1]:
                            maximum = healths[x]
                        else:
                            maximum = healths[x+1]
                    else:
                        end = x+1
                        count = count + 1
                        if healths[x+1] > max:
                            max = healths[x+1]

                x = x - 1
                #if x >= len(healths) - 2:
                    #break
                ""
                #x = x - 1
                
        #return healths

        # FIND UNSORTED ORDERED HEALTHS
        TRUEHEALTH = []
        order = []
        for d in range(len(healths)):
            order.append(ORPOS.index(positions[d]))
            #TRUEHEALTH.append(healths[ORPOS.index(positions[d])])
        pop = -1
        mindex = 0
        #print(order)
        for e in range(len(healths)):
            for g in range(len(healths)):
                if order[g] > pop:
                    min = order[g]
                    mindex = g
                    break
            for f in range(len(healths)):
                if order[f] < min and order[f] > pop:
                    min = order[f]
                    mindex = f
            TRUEHEALTH.append(healths[mindex])
            pop = min




        return TRUEHEALTH
        """

        
290