[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 k where 1 <= 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:

Input: arr = [3,2,4,1]
Output: [4,2,4,3]
Explanation: 
We perform 4 pancake flips, with k values 4, 2, 4, and 3.
Starting state: arr = [3, 2, 4, 1]
After 1st flip (k = 4): arr = [1, 4, 2, 3]
After 2nd flip (k = 2): arr = [4, 1, 2, 3]
After 3rd flip (k = 4): arr = [3, 2, 1, 4]
After 4th flip (k = 3): arr = [1, 2, 3, 4], which is sorted.

Example 2:

Input: arr = [1,2,3]
Output: []
Explanation: The input is already sorted, so there is no need to flip anything.
Note that other answers, such as [3, 3], would also be accepted.

Constraints:

  • 1 <= arr.length <= 100
  • 1 <= arr[i] <= arr.length
  • All integers in arr are unique (i.e. arr is a permutation of the integers from 1 to arr.length).

煎饼排序。

给你一个整数数组 arr ,请使用 煎饼翻转 完成对数组的排序。

一次煎饼翻转的执行过程如下:

选择一个整数 k ,1 <= k <= arr.length
反转子数组 arr[0...k-1](下标从 0 开始)
例如,arr = [3,2,1,4] ,选择 k = 3 进行一次煎饼翻转,反转子数组 [3,2,1] ,得到 arr = [1,2,3,4] 。

以数组形式返回能使 arr 有序的煎饼翻转操作所对应的 k 值序列。任何将数组排序且翻转次数在 10 * arr.length 范围内的有效答案都将被判断为正确。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/pancake-sorting
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题不涉及算法,我这里给出一个模拟的思路。我模拟的思路是,从 input 数组中最大的数字开始,逐个去把他们放到他们应该在的 index 上。

找到数组内最大的数字 max 的下标,设为 index,如果他不在数组未排序部分的最后,那么我们先把数组 [0, index] 这一段翻转,从而最大的数字在 index = 0 这个位置上了

再把数组中未排序的部分整体翻转,这样最大的数字 max 就去到了他应该去的 index

此时 max--,找下一个未被处理的最大的数字,同时记得把两次翻转涉及到的数字 K 放入结果集。注意这里的 K 不是下标。

时间O(n^2)

空间O(1)

Java实现

 1 class Solution {
 2     public List<Integer> pancakeSort(int[] arr) {
 3         List<Integer> res = new ArrayList<>();
 4         int n = arr.length;
 5         int max = n;
 6         for (int i = 0; i < n; i++) {
 7             // 找到当前未被sort的最大的数字的index
 8             int index = find(arr, max);
 9             // 把这个数字flip到数组最前面
10             reverse(arr, index);
11             // 把这个数字flip到数组最后面,这也是他最后该被放到的index
12             // 因为他是最大的数字
13             reverse(arr, max - 1);
14             // 把两个flip涉及到的数字加入结果集
15             res.add(index + 1);
16             res.add(max--);
17         }
18         return res;
19     }
20 
21     private int find(int[] nums, int max) {
22         for (int i = 0; i < nums.length; i++) {
23             if (nums[i] == max) {
24                 return i;
25             }
26         }
27         return -1;
28     }
29 
30     private void reverse(int[] nums, int index) {
31         int i = 0;
32         int j = index;
33         while (i < j) {
34             int temp = nums[i];
35             nums[i] = nums[j];
36             nums[j] = temp;
37             i++;
38             j--;
39         }
40     }
41 }

LeetCode 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/14843519.html