-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm735.py
27 lines (24 loc) · 948 Bytes
/
m735.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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]