Sort a permutation of 1 to n using only prefix reversals and return the sequence of flip lengths, using at most 10n flips. It tests greedy reasoning, careful translation between indices and flip lengths, and proving a bound on the number of operations.
Given an array of integers `arr`, sort it in ascending order using only pancake flips.
A pancake flip with parameter `k`, where `1 <= k <= arr.length`, reverses the prefix `arr[0..k-1]` (0-indexed).
Return the list of `k` values for a sequence of flips that sorts `arr`. Any sequence that sorts the array using at most `10 * arr.length` flips is accepted; it does not have to be the shortest one.
```python
def pancake_sort(arr: list[int]) -> list[int]:
```
Example 1:
```text
Input: arr = [3,2,4,1]
Output: [3,4,2,3,2]
```
Flipping 3 gives [4,2,3,1], flipping 4 gives [1,3,2,4], flipping 2 gives [3,1,2,4], flipping 3 gives [2,1,3,4], and flipping 2 gives [1,2,3,4]. Other valid sequences are accepted too.
Example 2:
```text
Input: arr = [1,2,3]
Output: []
```
The array is already sorted, so no flips are needed. Any sequence within the limit that leaves it sorted is also accepted.
```hint Where the largest value goes
The value n has exactly one correct final position. Count how few flips it takes to move it there from any index.
```
```hint Shrinking the problem
Once the last position holds the right value, look for flips that never touch it. What problem is left over?
```
### Constraints & Assumptions
- `1 <= arr.length <= 100`
- `1 <= arr[i] <= arr.length`
- All values are unique, so `arr` is a permutation of the integers 1 to `arr.length`.
- The answer may use at most `10 * arr.length` flips. A minimum-length sequence is not required.
### What a Strong Answer Covers
- A strategy that makes guaranteed progress with a fixed number of flips per step, and an argument that elements already placed are never disturbed.
- A bound on the total number of flips that shows the answer stays within `10 * arr.length`.
- Correct translation between 0-based indices and flip lengths, with each flip applied to a working copy so later positions are right.
- Time and space complexity.
- Edge cases: a single element, an already sorted array, and an element that is already in its final position.
### Follow-up Questions
- How would you find the minimum number of flips for a given array, and how far does that approach scale?
- Each pancake now has a burnt side that must face down in the final stack, and every flip also turns the flipped pancakes over. How does your algorithm change, and how many flips does it use?
- What changes if `arr` may contain duplicates or arbitrary integers instead of a permutation of 1 to n?
Overview: Sort a permutation of 1 to n using only prefix reversals and return the sequence of flip lengths, using at most 10n flips. It tests greedy reasoning, careful translation between indices and flip lengths, and proving a bound on the number of operations.
Given an array of integers arr, sort it in ascending order using only pancake flips.
A pancake flip with parameter k, where 1 <= k <= arr.length, reverses the prefix arr[0..k-1] (0-indexed).
Return the list of k values for a sequence of flips that sorts arr. Any sequence that sorts the array using at most 10 * arr.length flips is accepted; it does not have to be the shortest one.
def pancake_sort(arr: list[int]) -> list[int]:
Example 1:
Input: arr = [3,2,4,1]
Output: [3,4,2,3,2]
Flipping 3 gives [4,2,3,1], flipping 4 gives [1,3,2,4], flipping 2 gives [3,1,2,4], flipping 3 gives [2,1,3,4], and flipping 2 gives [1,2,3,4]. Other valid sequences are accepted too.
Example 2:
Input: arr = [1,2,3]
Output: []
The array is already sorted, so no flips are needed. Any sequence within the limit that leaves it sorted is also accepted.
Constraints & Assumptions
1 <= arr.length <= 100
1 <= arr[i] <= arr.length
All values are unique, so
arr
is a permutation of the integers 1 to
arr.length
.
The answer may use at most
10 * arr.length
flips. A minimum-length sequence is not required.
What a Strong Answer Covers Guidance
A strategy that makes guaranteed progress with a fixed number of flips per step, and an argument that elements already placed are never disturbed.
A bound on the total number of flips that shows the answer stays within
10 * arr.length
.
Correct translation between 0-based indices and flip lengths, with each flip applied to a working copy so later positions are right.
Time and space complexity.
Edge cases: a single element, an already sorted array, and an element that is already in its final position.
Follow-up Questions Guidance
How would you find the minimum number of flips for a given array, and how far does that approach scale?
Each pancake now has a burnt side that must face down in the final stack, and every flip also turns the flipped pancakes over. How does your algorithm change, and how many flips does it use?
What changes if
arr
may contain duplicates or arbitrary integers instead of a permutation of 1 to n?