Asteroid Collision (IMC Sydney π)
In this game, there are a number asteroids arranged in line. These asteroids can all move either left or right on the line and all asteroids move at the same speed. Each asteroid also has a size associated with it. If two asteroids that are next to each other move towards each other, they will collide. In this case, the larger asteroid will destroy the smaller one. If both asteroids have the same size, then both asteroids are destroyed. The purpose of this game is to determine which asteroids remain after all possible collisions occur.
Thus, given an array size of length n, where size[i] is the size of the ith asteroid and a second array direction of size n where direction[i] is the direction of the ithasteroid where 0 indicates moving left and 1 indicates moving right.
Given these inputs, find which asteroids remain after all collisions have taken place. Return an array containing the sizes of the remaining asteroids (in order). If no asteroids remain, then return an empty array.
1Example 1
2Example 2
3Example 3
Constraints
Limits and guarantees your solution can rely on.
A mysterious secrete for now π