Trains the technique from
LeetCode 969Pancake SortingThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A cook has a stack of pancakes described by arr, where arr[0] is the pancake on top and arr[len(arr) - 1] the one on the bottom. The pancakes carry the sizes 1 through len(arr), each exactly once.
A flip of depth d slides a spatula under the d-th pancake from the top and turns that whole top portion over, reversing the order of its first d pancakes.
The cook sorts the stack so sizes increase from top to bottom, using this routine. Take the largest size not yet settled. If it is not already in its final place, flip at its depth to bring it to the top, then flip at the depth of its final place to drop it there. A flip is never performed when it would change nothing. Repeat with the next largest size.
Return the depths of the flips the routine performs, in the order it performs them.
Example 1
The size 5 is already on top, so the routine flips at depth 5 to send it to the bottom, then goes on to settle the size 4, and so on down.
Example 2
Both pancakes already sit in their final places, so no flip is performed.
Example 3
The size 2 is already on top, so no flip is needed to bring it up, and a flip of depth 2 sends it to the bottom.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def spatula_flips(arr: list[int]) -> list[int]:public List<Integer> spatulaFlips(int[] arr)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.