Merging Balls in a Tube
Balls are dropped one at a time into a narrow tube, and balls lists their numbers in drop order. When a ball lands on a ball with the same number, the two merge into one ball with double the number. That ball can merge again with the ball below it, and so on.
Return the numbers of the balls in the tube at the end, from the bottom to the top, or an empty list if no ball was dropped.
Example 1
balls [2,2,4,8,3]
returns [16,3]
The two 2s make a 4, which merges with the 4 into an 8, which merges with the 8 into a 16. The 3 stays on top.
Example 2
balls [1,2,1,1]
returns [1,4]
The last 1 merges with the 1 below into a 2, which merges with the 2 below into a 4.
Constraints
0 <= len(balls) <= 1001 <= balls[i] <= 64
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.