• Difficulty: Medium

Problem

Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, then reverse the order of the first k elements of A. We want to perform zero or more pancake flips (doing them one after another in succession) to sort the array A.

Return the k-values corresponding to a sequence of pancake flips that sort A. Any valid answer that sorts the array within 10 * A.length flips will be judged as correct.

Example 1:

Input: [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: A = [3, 2, 4, 1]
After 1st flip (k=4): A = [1, 4, 2, 3]
After 2nd flip (k=2): A = [4, 1, 2, 3]
After 3rd flip (k=4): A = [3, 2, 1, 4]
After 4th flip (k=3): A = [1, 2, 3, 4], which is sorted.

Example 2:

Input: [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.

Note:

  1. 1 <= A.length <= 100
  2. A[i] is a permutation of [1, 2, ..., A.length]

Related Topics

Array, Sort

Solution

题目大意是给一个数组,用翻转数组前 N 项的方式对其进行排序。由于是翻转前 N 项,所以可以让数组中最大的数通过此法先放到数组末尾,从后往前完成排序。又由于题目指出了给出的数组一定是从 1 到 N 的全排列,因此可以简单地定最大值 maxA.length,每排出一个让 max--,当 max == 1 时停止操作。代码如下:

public class Solution
{
public IList<int> PancakeSort(int[] A)
{
int max = A.Length;
List<int> ret = new List<int>();
while(max != 1)
{
int i = Array.IndexOf(A, max);
if(i == max - 1)
{
// 情况一:max 已经有序,直接进行下一个循环
max--;
continue;
}
else if(i == 0)
{
// 情况二:max 为数组的首个元素,进行一次前 max 个元素翻转
ret.Add(max);
ArrayReverse(A, max);
}
else
{
// 其余情况:进行两步操作,第一步将其放到数组首位,第二步归位
ret.Add(i + 1);
ret.Add(max);
ArrayReverse(A, i + 1);
ArrayReverse(A, max);
}
max--;
}
return ret;
} static void ArrayReverse<T>(T[] arr, int len)
{
int left = 0, right = len - 1;
while (left < right)
{
T temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
}

当然,题目只是要求找到一组可行解即可(而且翻转次数不超过 10 倍的数组长度),这个条件是比较宽泛的,题解做法可以保证一定能解出来,且翻转次数应该是小于 2 倍数组长度(没有验证,如有错误还请指出)。若是题目改成使用最少的翻转次数完成排序,则题目就变成了所谓的“烙饼问题”,这篇文章对这个问题给出了相应的解答。

[Solution] 969. Pancake Sorting的更多相关文章

  1. LC 969. Pancake Sorting

    Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...

  2. 【leetcode】969. Pancake Sorting

    题目如下: Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.len ...

  3. 【LeetCode】969. Pancake Sorting 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟法 日期 题目地址:https://leetco ...

  4. Leetcode 969. Pancake Sorting

    每次找到当前最大数,转两下把最大数转到最右边.重复这个操作,直到都转完. 时间复杂度O(n**2) class Solution(object): def pancakeSort(self, A): ...

  5. [Swift]LeetCode969.煎饼排序 | Pancake Sorting

    Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...

  6. Pancake Sorting LT969

    Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...

  7. 118th LeetCode Weekly Contest Pancake Sorting

    Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...

  8. 【LeetCode】Pancake Sorting(煎饼排序)

    这道题是LeetCode里的第969道题. 题目要求: 给定数组 A,我们可以对其进行煎饼翻转:我们选择一些正整数 k <= A.length,然后反转 A 的前 k 个元素的顺序.我们要执行零 ...

  9. 【LEETCODE】57、数组分类,适中级别,题目:969、442、695

    package y2019.Algorithm.array.medium; import java.util.ArrayList; import java.util.List; /** * @Proj ...

随机推荐

  1. golang实现的简单优先队列

    下面是golang实现的简单优先队列,参考信息可以查看https://golang.org/pkg/container/heap/或者https://golang.google.cn/pkg/cont ...

  2. three.js的组合与合并,raycaster射线无法获取group

    1.组合 创建一个组非常简单,在组中添加子元素的效果是,你可以对组进行移动.缩放和变形,而所有的子对象都会受到影响.使用组的时候,你依然可以引用.修改每一个单独的几何体.但是,使用raycaster射 ...

  3. SQL对于 小数处理的小结

    DECLARE @digital INT --截断小数位 ,,)),@digital) AS 截断小数位 --上抛小数位 ,,)),@digital) AS 上抛小数位 SELECT CEILING ...

  4. eclipse--常见问题

    学习java的都知道这个编辑器,但这个编辑器的有很多功能很多人不知道怎么用,我系统的整理一下我学习过程中遇到的过的问题 1.eclipse如何导入external jar包?        参考:ht ...

  5. webapi xml序列化删除<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">标签

    在WebApiConfig.Register中加入config.Formatters.Remove(config.Formatters.XmlFormatter); 移除默认的xml序列化器

  6. 记录2-在mac上安装ubuntu 16.04 LTS

    前几天升级了我用了六七年mac硬件,内存由4G变为8G,硬盘也换成1T SSD,索性把一直想装的ubuntu也装了,方便温习下以前的工作环境. 我比较喜欢LTS的版本,所以安装了16.04. 主要步骤 ...

  7. Verilog语言

    for循环应用 1.复位寄存器组 例如有32个寄存器,需要异步复位 always@(posedge clk or negedge rst_n) begin if (rst_n == 1'b0) beg ...

  8. Android 开发 CoordinatorLayout 协调者布局 与 ConstraintLayout约束布局 两者的关系

    在摸索新技术是发现CoordinatorLayout 与 ConstraintLayout 会有冲突关系,所以就研究了一下他们之间的不兼容,被影响的方面.其实某种程度上来说是CoordinatorLa ...

  9. linux gcc 静态 动态链接库

    静态链接库 首先生成依赖函数的目标文件 gcc -c source1.c source2.c; 然后归档目标文件到静态库 ar -rcs libYourID.a obj1.o obj2.o; 然后我们 ...

  10. centos mysql 默认是区分大小写的,修改成不区分大小写

    修改mysql为不区分大小写设置: [root@test-huanqiu ~]# vim /etc/my.cnf //添加下面一行设置 .... [mysqld] lower_case_table_n ...