Asteroid Collision
Try to solve the Asteroid Collision problem.
We'll cover the following
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:
-
asteroids.length
-
asteroids[i]
-
asteroids[i]
Examples
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.