Skip to content
This repository was archived by the owner on Jul 30, 2023. It is now read-only.

Commit ca450ea

Browse files
add 0735-asteroid-collision.py
1 parent 5ba127b commit ca450ea

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

python/0735-asteroid-collision.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
3+
stack = []
4+
5+
for a in asteroids:
6+
while stack and a < 0 and stack[-1] > 0:
7+
diff = a + stack[-1]
8+
if diff > 0:
9+
a = 0
10+
elif diff < 0:
11+
stack.pop()
12+
else:
13+
a = 0
14+
stack.pop()
15+
if a:
16+
stack.append(a)
17+
18+
return stack

0 commit comments

Comments
 (0)