Asteroid Collision

Try to solve the Asteroid Collision problem.

Statement

Given an array of integers, asteroids, which represents asteroids in a row, the absolute integer value of each asteroid represents its size, and the sign represents the direction of its movement. A positive sign signifies that the asteroid is moving toward the right direction, while a negative sign indicates that it is moving toward the left direction. All asteroids move at the same speed.

We need to determine the final state of the asteroids after all the collisions have occurred. When two asteroids of different sizes collide, the smaller one will be destroyed. If both asteroids are of the same size, both will be destroyed.

Note: Asteroids moving in the same direction will never collide.

Constraints:

  • 22 \leq asteroids.length 103\leq 10^3

  • 103-10^3 \leq asteroids[i] 103\leq 10^3

  • asteroids[i] 0\neq 0

Examples

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.