• 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. securecrt 方向键乱码解决

    1 下载安装包rlwrap: rlwrap-0.30.tar.gz     http://utopia.knoware.nl/~hlub/uck/rlwrap/   2  install rlwrap ...

  2. Warning: mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password]报错解决方法

    错误: 解决方法:

  3. python调试pdb

    开始调试 python3 -m pdb pdb.py break 或 b line_num 设置断点设置断点 continue 或 c继续执行程序 list 或 l查看当前行的代码段 step 或 s ...

  4. python爬虫学习笔记(二)——基础篇之爬虫基本原理

    1.什么是爬虫? 请求网站并提取数据的自动化程序 2.爬虫基本流程 2.1发起请求 通过HTTP库向目标站点发起请求,即发起一个Request,请求可以包含额外的headers等信息,等待服务器响应: ...

  5. LeetCode【110. 平衡二叉树】

    对于平衡二叉树,就是左右深度相差1 就可以另外弄一个函数,计算深度,然后, 在原函数上进行比较深度是否相差1,再输出true or false. 至于迭代就可以,比较完左右节点,再比较各自的左右节点. ...

  6. LeetCode【108. 将有序数组转换为二叉搜索树】

    又是二叉树,最开始都忘记了二叉搜索树是什么意思,搜索了一下: 二叉搜索树:左节点都小于右节点,在这里就可以考虑将数组中的中间值作为根节点 平衡二叉树:就是左右节点高度不大于1 树就可以想到递归与迭代, ...

  7. 4、redis 分布式锁

    1. 前言 关于分布式锁的实现,目前常用的方案有以下三类: 数据库乐观锁: 基于分布式缓存实现的锁服务,典型代表有 Redis 和基于 Redis 的 RedLock: 基于分布式一致性算法实现的锁服 ...

  8. C&C++类型定义typedef

    1.声明 1.1结构声明: struct { int n; double x,y; }; 1.2带结构标志的声明 struct point{ double x,y; }; 1.3定义结构类型 1.3. ...

  9. strace参数

    strace参数 -c 统计每一系统调用的所执行的时间,次数和出错的次数等. -d 输出strace关于标准错误的调试信息. -f 跟踪由fork调用所产生的子进程. -ff 如果提供-o filen ...

  10. 201772020113李清华《面向对象程序设计(java)》第十二周学习总结

    1.实验目的与要求 (1) 掌握Java GUI中框架创建及属性设置中常用类的API: (2) 掌握Java GUI中2D图形绘制常用类的API: (3) 了解Java GUI中2D图形中字体与颜色的 ...