← All problems

Asteroid Collision

medium

Given an array asteroids where each value's sign gives its direction (positive = right, negative = left) and its magnitude gives its size, simulate all collisions: when two meet, the smaller explodes; equal sizes both explode. Asteroids moving the same direction never meet. Return the state after all collisions settle.

A stack works because only the most recently surviving right-mover can ever meet the next left-mover.

Example

Input: asteroids = [5, 10, -5]
Output: [5, 10]        // -5 hits 10, 10 survives, -5 is destroyed
Loading...