Leetcode 735 Solution

This article provides solution to leetcode question 735 (asteroid-collision)

https://leetcode.com/problems/asteroid-collision

Solution

class Solution:
    def asteroidCollision(self, asteroids: List[int]) -> List[int]:
        s = []
        for ast in asteroids:
            if not s:
                s.append(ast)
            else:
                if ast > 0:
                    s.append(ast)
                else:
                    while s and s[-1] > 0 and s[-1] < -ast:
                        s.pop(-1)

                    if s and s[-1] == -ast:
                        s.pop(-1)
                        continue

                    if not s or s[-1] < 0:
                        s.append(ast)
        return s