Skip to content

Latest commit

 

History

History
73 lines (58 loc) · 2.29 KB

_2751. Robot Collisions.md

File metadata and controls

73 lines (58 loc) · 2.29 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 13, 2024

Last updated : July 13, 2024


Related Topics : Array, Stack, Sorting, Simulation

Acceptance Rate : 57.03 %


Version 1

    Notes
    Conditions
    - Collide = if occupy same spot
        - Same HP = both die
        - Else = lower dies other health -= 1

    - No more collisions: if all are going same direction 
                          or if none going towards each other

Note: this looks and feels very similar to 735 Asteroid Collisions (did it a today, the next day, and realized lol).


Solutions

Python

# By far not optimized but I wanted to see if it would pass and 
# I was aiming for speed. Managed to get an AC with this code in 14m:48s :)

class Solution:
    def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]:
        # (position, direction, health, origIndx)
        posDir = sorted([(pd[0], pd[1], pd[2], origIndx) for origIndx, pd in enumerate(zip(positions, directions, healths))], key=lambda x: x[0])

        popped = []
        escaped = []
        while posDir :
            if posDir[-1][1] == 'L' :
                popped.append(posDir.pop())
            elif not popped : # R and no L to the right
                escaped.append(posDir.pop())
            else :
                if posDir[-1][2] > popped[-1][2] :
                    popped.pop()
                    posDir[-1] = (posDir[-1][0], posDir[-1][1], posDir[-1][2] - 1, posDir[-1][3])
                elif posDir[-1][2] < popped[-1][2] :
                    posDir.pop()
                    popped[-1] = (popped[-1][0], popped[-1][1], popped[-1][2] - 1, popped[-1][3])
                else :
                    popped.pop()
                    posDir.pop()

        return [x[2] for x in sorted(popped + escaped, key=lambda x: x[3])]