pancake-sorting
LeetCode Problem 969 "Pancake Sorting" is an interesting real-world problem: Imagine you have n pancakes of different sizes on a plate. Using a spatula, how can you flip the pancakes a few times to sort them by size (smallest on top, largest on bottom)?

Think of flipping a stack of pancakes with a spatula. There is a rule: each time, you can only flip the top few pancakes.

Our task is: How can we use an algorithm to find a sequence of flips to make the pancake stack sorted?
First, we need to model the problem. We use an array to represent the pancake stack:
LeetCode 969. Pancake Sorting
Given an array of integers arr, sort the array by performing a series of pancake flips.
In one pancake flip we do the following steps:
Choose an integer
kwhere1 <= k <= arr.length.Reverse the sub-array
arr[0...k-1](0-indexed).
For example, if arr = [3,2,1,4] and we performed a pancake flip choosing k = 3, we reverse the sub-array [3,2,1], so arr = [1,2,3,4] after the pancake flip at k = 3.
Return *an array of the k-values corresponding to a sequence of pancake flips that sort *arr. Any valid answer that sorts the array within 10 * arr.length flips will be judged as correct.
Example 1:**
Example 2:**
Constraints:
1 <= arr.length <= 1001 <= arr[i] <= arr.lengthAll integers in
arrare unique (i.e.arris a permutation of the integers from1toarr.length).
How do we solve this problem? Like in the article Recursively Reverse Part of a Linked List, we need to use recursion.
1. Idea Analysis
Why does this problem have the property of recursion? For example, we can make a function like this:
If we find the largest pancake among the first n pancakes, and flip it to the bottom:

Now the problem size is smaller, so we can recursively call pancakeSort(A, n-1):

Next, for these n - 1 pancakes, how to sort them? Again, find the largest, put it at the bottom, and call pancakeSort(A, n-1-1)...
You see, this is recursion. To summarize:
Find the largest pancake among the
npancakes.Move this largest pancake to the bottom.
Recursively call
pancakeSort(A, n - 1).
Base case: When n == 1, there is only one pancake, so no flips are needed.
Now, one last question: How do we move a certain pancake to the bottom?
It's simple. For example, if the 3rd pancake is the largest and you want to move it to the bottom (the nth position):
Use the spatula to flip the top 3 pancakes. Now the largest is at the top.
Flip the top
npancakes. Now the largest is at the bottom.
With these two steps, you can write the solution. The problem also asks us to return the flip operation sequence. We just need to record each flip.
2. Code Implementation
Just write the above idea in code. Note: array index starts from 0, but the answer should use 1-based indices.
With the explanation above, the code should be clear.
The time complexity is easy to calculate. There are n recursive calls, and each call has a for loop of O(n), so total time complexity is O(n^2).
Finally, let's think about a question: In this solution, the length of the flip sequence is 2(n - 1), because each recursion does 2 flips, and there are n layers of recursion. But the base case does not flip, so the total number is 2(n - 1).
Clearly, this is not the optimal (shortest) sequence. For example, with pancakes [3,2,4,1], our algorithm gives flips [3,4,2,3,1,2]. But the shortest flip sequence is [2,3,4]:
If you are asked to find the shortest flip sequence to sort the pancakes, how would you solve it? What is the core idea to find the optimal solution? What algorithm should you use? Feel free to share your thoughts.
Last updated