735. Asteroid Collision - cocoder39/coco39_LC GitHub Wiki
735. Asteroid Collision
- assuming there exists a dummy asteroid which is moving left
- if the actual first is moving left, it can be appended
- if the actual first is moving right, it can be appended
- assuming there exists a dummy asteroid which is moving right
- if the actual first is moving left, there could be collision (red flag so discard this option)
- if the actual first is moving right, it can be appended
class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
res = []
for asteroid in asteroids:
exploded = False
while res and asteroid < 0 < res[-1]:
if abs(asteroid) > abs(res[-1]):
res.pop()
elif abs(asteroid) == abs(res[-1]):
exploded = True
res.pop()
break
else:
exploded = True
break
if not exploded:
res.append(asteroid)
return res