Flip the Pancakes
stack lists a stack of pancakes from the bottom to the top, and each number is the size of a pancake. One flip slides a spatula under the top k pancakes and turns them over together, which reverses their order. flips lists the values of k, one per flip, in the order the flips are made.
Return the stack after all the flips, from the bottom to the top. A flip with k larger than the stack turns the whole stack over.
Example 1
stack [1,2,3,4]
flips [2]
returns [1,2,4,3]
The top two pancakes, 3 and 4, change places.
Example 2
stack [1,2,3,4]
flips [2,4]
returns [3,4,2,1]
The first flip gives [1, 2, 4, 3]. The second turns all four over.
Example 3
stack [5,6,7]
flips []
returns [5,6,7]
No flips, so the stack stays as it is.
Constraints
1 <= len(stack) <= 201 <= stack[i] <= 990 <= len(flips) <= 201 <= flips[i] <= 25
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Stacks and queues problems
- Last In, First OutBeginner
- Notes on the BoardBeginner
- One-Bay Car WashBeginner
- Round the QueueBeginner
- Served in OrderBeginner
- Stack After CommandsBeginner
- Typing With UndoBeginner
- Card WarEasy