• 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. python基础知识16---函数补充

    一 数学定义的函数与python中的函数 初中数学函数定义:一般的,在一个变化过程中,如果有两个变量x和y,并且对于x的每一个确定的值,y都有唯一确定的值与其对应,那么我们就把x称为自变量,把y称为因 ...

  2. C++ 重定义、重载、覆盖

    想要用好C++继承和类自身函数实现就必须了解C++得三个概念重定义(redefine).重载(overload).重写(override). 一 基本感念 1 重定义(redefine) 派生类对基类 ...

  3. kali syn洪水攻击实例

    kali 172.30.2.241 受攻击:172.30.2.242 syn攻击造成对方cpu跑满 基本上丧失工作能力 攻击语句

  4. 20175311 《Java程序设计》第三周学习总结

    20175311 2018-2019-2 <Java程序设计>第3周学习总结 教材学习内容总结 在蓝墨云中的教程里学习了如何安装IDEA,并且尝试了自己破解IDEA 主要在看书时,对jav ...

  5. MySQL-exists和in的区别

    SQL查询中in和exists的区别分析 对于一些不可不免的查询场景,我们难免要用到子查询 那么in和exists那个的效率更高一点呢 SQL查询中in和exists的区别分析 select * fr ...

  6. C# Directory.Exists() 文件存在但返回一直为false

    备注:这是一个低级错误,起始真正的原因不是访问权限的问题. 真正的原因是:这个程序要读取远程电脑上共享文件夹里的文件,但是没有远程访问代码,导致找不到相关的目录.所以才报错! 查询一个文件,但程序突然 ...

  7. Linux基础之常用命令整理(一)

    Linux 操作系统的安装 如今比较流线的linux操作系统 Centos Redhat  Fedora Ubuntu, 安装操作系统的提前是要有操作系统的镜像文件(.iso文件)并且必须为系统指定一 ...

  8. day2.jmeter简单压测,下载文件,Charles手机抓包准备

    一.压测 压测衡量一个系统的好坏:1.tps每秒钟处理的事物数,2.qps响应时间 添加聚合报告,更改线程组,运行接口请求 **添加压力机 1.首先确保都在同一网段 2.其他电脑要先启动jmeter- ...

  9. Java笔记Spring(五)

    C:\apache-tomcat-8.0.36\bin\catalina.bat run [2018-05-23 02:30:31,657] Artifact demo-springmvc:war e ...

  10. add-migration Build failed.

    >add-migration JczInfoDateTimeBuild failed. 1编译解决方案,看是否报错,不管哪个有错误都会迁移失败 2重启vs