Asteroid Collision
Problem statement
You are given an integer array asteroids representing asteroids in a row. Absolute value is size, and sign is direction: positive moves right and negative moves left. All asteroids move at the same speed.
When two asteroids moving toward each other collide, the smaller one explodes. If their sizes are equal, both explode. Asteroids moving in the same direction never collide.
Return the asteroids remaining after every collision, in original left-to-right order.
Function
asteroidCollision(asteroids: int[]) → int[]Examples
Example 1
asteroids = [5,10,-5]return = [5,10]Asteroid 10 destroys -5; 5 never collides.
Example 2
asteroids = [8,-8]return = []The approaching asteroids have equal size, so both explode.
Example 3
asteroids = [10,2,-5]return = [10]-5 destroys 2, then 10 destroys -5.
Constraints
1 <= asteroids.length <= 100000-10^9 <= asteroids[i] <= 10^9asteroids[i] != 0