Asteroid Collision LeetCode solution in Python

1
# Runtime: 103 ms (Top 70.1%) | Memory: 17.56 MB (Top 44.2%)
2

3

4
class Solution:
5
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
6
stack = []
7
for a in asteroids:
8
while stack and stack[-1] > 0 > a:
9
if stack[-1] < abs(a):
10
stack.pop()
11
continue
12
elif stack[-1] == abs(a):
13
stack.pop()
14
break # this means asteroid must be destroyed (not add to stack in else statement below)
15
else:
16
stack.append(a)
17

18
return stack

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0