Asteroid Collision LeetCode solution in Python
1# Runtime: 103 ms (Top 70.1%) | Memory: 17.56 MB (Top 44.2%)234class Solution:5def asteroidCollision(self, asteroids: List[int]) -> List[int]:6stack = []7for a in asteroids:8while stack and stack[-1] > 0 > a:9if stack[-1] < abs(a):10stack.pop()11continue12elif stack[-1] == abs(a):13stack.pop()14break # this means asteroid must be destroyed (not add to stack in else statement below)15else:16stack.append(a)1718return stack