FunctionRacerProblemsRace

Crate Pile

BeginnerStacks and queuesPython, JavaScript, TypeScript, Java, C++

Crates arrive one at a time, and weights lists them in arrival order. Each crate goes on top of a single pile, but first every lighter crate at the top of the pile is taken off and shipped away, until the top crate is at least as heavy as the new one or the pile is empty. Crates of equal weight both stay.

Return the weights left on the pile from bottom to top, or an empty list if no crate arrived.

Example 1

weights [5,3,4,4,2,6,1]
returns [6,1]

The 4 takes the 3 off, the second 4 stays on the first, and the 6 clears the whole pile. The 1 then sits on the 6.

Example 2

weights [9,7,7,3]
returns [9,7,7,3]

No crate is heavier than the crate it lands on, so every crate stays.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems