25/36
Part 7 of 8 · The worked exampleLearn and do

A worked example: rocks that collide

The prompt, in the shape of Asteroid Collision, which is on the compiled LeetCode list for OpenAI (1 source): a row of rocks drifts along a line, all at the same speed. Each rock is a nonzero integer. Its size is the absolute value, and its sign is its direction: positive to the right, negative to the left. When two rocks meet, the smaller one breaks up, and if they're the same size both do. Return the rocks left once nothing else can meet, in order. Here, [4, 7, -3, 2, -9, 6, -6, -1, 5].

Only a right-mover with a left-mover somewhere to its right can collide

Predict

Rocks 4, 7, -3, 2, -9, 6, -6, -1, 5: positive moves right, negative left. When two meet, the smaller breaks, and equal ones both break. Which survive?

Pick an answer above to see the rest