735. Asteroid Collision
All prompts are owned by LeetCode. To view the prompt, click the title link above.
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.
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]