Gamified challenge · Stacks
asteroids[i] is the asteroid at position i on the belt, listed left to right. Its absolute value is its size and its sign is its direction: positive drifts right, negative drifts left. All of them move at the same speed, so two asteroids only meet when a right-mover is somewhere to the left of a left-mover.
When two asteroids meet, the smaller one shatters; if they are the same size, both shatter. Asteroids moving the same way never meet. Write survivors(asteroids) that returns the asteroids still flying after every crash, in their left-to-right order, with their original signs.
For [5, 10, -5], -5 hits 10 and shatters, so the answer is [5, 10]. For [10, 2, -5], -5 breaks 2 and then breaks against 10: the answer is [10]. Sizes are never 0.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google