Skip to content

Latest commit

 

History

History
59 lines (44 loc) · 1.59 KB

_735. Asteroid Collision.md

File metadata and controls

59 lines (44 loc) · 1.59 KB

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

Back to top


First completed : July 14, 2024

Last updated : July 14, 2024


Related Topics : Array, Stack, Simulation

Acceptance Rate : 44.72 %


Very VERY similar to the daily question from yesterday called robot collisions.


Solutions

Python

class Solution:
    def asteroidCollision(self, asteroids: List[int]) -> List[int]:
        output = []
        stk = []

        while asteroids :
            # going right
            if asteroids[-1] > 0 :
                # while current asteroid is larger than the ones going left
                while stk and stk[-1] + asteroids[-1] > 0 :
                    stk.pop()
                
                # if same size
                if stk and stk[-1] == -asteroids[-1] :
                    asteroids.pop()
                    stk.pop()
                # no astroids not destroyed going left
                elif not stk :
                    output.append(asteroids.pop())
                # asteroid going left is larger than current
                else : # smaller
                    asteroids.pop()
            # asteroid going left
            else :
                stk.append(asteroids.pop())

        return stk[::-1] + output[::-1]