We are given an array asteroids of integers representing asteroids in a row. The indices of the asteroids in the array represent their relative position in space.
For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.
Find the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.
Example 1:
Input: asteroids = [5,10,-5]
Output: [5,10]
Explanation: The 10 and -5 collide, resulting in 10. The 5 and 10 never collide.
This problem is a classic stack simulation. Scan the asteroids from left to right and keep the surviving asteroids in a stack. A collision can only happen when the stack top moves right and the current asteroid moves left. Compare their absolute values to decide whether one explodes or both disappear. This leads to an O(n) solution with clean handling of all chain-collision cases.